Python List pop()

Python List pop() is a built-in function that removes the item at the specified index from the list and returns the removed item. If the index is not passed, the last item is popped out and removed from the list.

In this tutorial, we will learn about the Python list pop() method with the help of examples.

Syntax of List pop() 

The syntax of the pop() method is:

list.pop(index)

pop() Parameters

The pop() method takes a single parameter.

  • index (optional)- The index value of the element that needs to be popped and removed from the list. 

Notes:

  • If the index is not passed to the method, the default index -1 gets passed as an argument and removes the last element from the list.
  • If the index passed to the method is not in the range, the pop() method will raise IndexError: pop index out of range exception.

Return Value from List pop()

The pop() method returns the item which gets popped and removed from the list.

Example 1: Pop item at the given index from the list

In this example, the pop() method will remove the item at the index position 4 and returns the element that is popped.

Note: The index starts from 0 and not 1 in the list traversal. IN the below example we need to pop the 5th item and hence we need to pass the index as 4.

# list of laptops
laptops = ["Dell","Lenovo","HP","Apple","Acer","Asus"]

# remove and return Acer laptop at the index position 4
item_removed= laptops.pop(4)

# removed item
print("The item removed is ", item_removed)

# updated list
print("The updated list is ",laptops)

Output

The item removed is  Acer
The updated list is  ['Dell', 'Lenovo', 'HP', 'Apple', 'Asus']

Example 2: pop() without an index, and for negative indices

Let us look at some examples of pop() method without passing any index and how it works with negative indices.


# list of laptops
laptops = ["Dell","Lenovo","HP","Apple","Acer","Asus"]

# remove and return the last item when index is not passed
item_removed= laptops.pop()
print("The item removed is ", item_removed)
print("The updated list is ",laptops)


# remove and return the last item
item_removed= laptops.pop(-1)
print("The item removed is ", item_removed)
print("The updated list is ",laptops)

# remove and return the last 3rd item
item_removed= laptops.pop(-3)
print("The item removed is ", item_removed)
print("The updated list is ",laptops)

Output

The item removed is  Asus
The updated list is  ['Dell', 'Lenovo', 'HP', 'Apple', 'Acer']

The item removed is  Acer
The updated list is  ['Dell', 'Lenovo', 'HP', 'Apple']

The item removed is  Lenovo
The updated list is  ['Dell', 'HP', 'Apple']

We can also use remove() method to remove the items from the list and also we can use del statement to remove an item or slices from the list.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like