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.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Split List Into Chunks

Python Split list into chunks

Table of Contents Hide Python Split list into chunksMethod 1: Using a For-LoopMethod 2: Using the List Comprehension MethodMethod 3: Using the itertools MethodMethod 4: Using the NumPy MethodMethod 5:…
View Post
Python String Casefold()

Python String casefold()

Table of Contents Hide casefold() Syntaxcasefold() Parametercasefold() Return ValueDifference between casefold and lower in PythonExample 1: Convert string to lowercase using casefold() Example 2: Compare strings using casefold() Python String casefold() method…
View Post
Python String Capitalize()

Python String capitalize()

Table of Contents Hide capitalize() Syntaxcapitalize() ParameterReturn Value from capitalize()Example: Capitalize a string in Python Python string capitalize() method will convert the first letter in a string to uppercase and keep…
View Post
Python Check If String Is Empty Or Not

Check if string is empty or not in Python

Table of Contents Hide Python program to check if string is empty or notUsing len() funtionUsing not operatorUsing not+str.strip() method Using not + str.isspace method In this article, you will learn…
View Post