Using the pop() Method in Python

python list pop

pop() is a built-in method in Python that is used to remove and return an element from a list. It takes an optional index value as an argument, which specifies the position of the element to be removed. If no index value is provided, it removes and returns the last element of the list. The syntax for the pop() method is as follows:

list.pop([index])

Here, list is the name of the list from which the element is to be removed, and index is the optional index value that specifies the position of the element. If no index value is provided, the last element of the list is removed.

How to Use Python List pop()?

Now that we know what pop() is, let’s see how it can be used in various scenarios.

Example 1: Removing the Last Element

The most common use case of pop() is to remove the last element of a list. Here’s an example:

fruits = ['apple', 'banana', 'cherry', 'date']
last_fruit = fruits.pop()
print('Removed Fruit:', last_fruit)
print('Updated List:', fruits)

Output:

Removed Fruit: date
Updated List: ['apple', 'banana', 'cherry']

In this example, we have a list of fruits, and we want to remove the last fruit from the list. We simply call the pop() method without any argument, and it removes and returns the last element of the list, which is ‘date’. We store this value in a variable called last_fruit and print it. Then we print the updated list, which has the last element removed.

Example 2: Removing an Element by Index

We can also use pop() to remove an element from a list by specifying its index value. Here’s an example:

fruits = ['apple', 'banana', 'cherry', 'date']
third_fruit = fruits.pop(2)
print('Removed Fruit:', third_fruit)
print('Updated List:', fruits)

Output:

Removed Fruit: cherry
Updated List: ['apple', 'banana', 'date']

In this example, we have a list of fruits, and we want to remove the third fruit from the list, which has an index value of 2 (remember, indexing starts from 0). We call the pop() method with the index value of the element to be removed, which is 2. It removes and returns the element ‘cherry’, which we store in a variable called third_fruit. Then we print the updated list, which has the third element removed.

Example 3: Removing Multiple Elements

We can also use pop() to remove multiple elements from a list by calling it multiple times in a loop. Here’s an example:

fruits = ['apple', 'banana', 'cherry', 'date']
removed_fruits = []
for i in range(2):
    removed_fruit = fruits.pop()
    removed_fruits.append(removed_fruit)
print('Removed Fruits:', removed_fruits)
print('Updated List:', fruits)

Output:

Removed Fruits: ['date', 'cherry']
Updated List: ['apple', 'banana']

In this example, we have a list of fruits, and we want to remove the last two fruits from the list. We create an empty list called removed_fruits to store the removed fruits. Then we call the pop() method twice in a loop, which removes the last two elements of the list and stores them in the removed_fruit variable. We append this variable to the removed_fruits list in each iteration. Finally, we print the removed_fruits list and the updated fruits list.

Example 4: Removing Elements from a Nested List

We can also use pop() to remove elements from a nested list. Here’s an example:

nested_list = [['apple', 'banana'], ['cherry', 'date']]
removed_element = nested_list[1].pop(1)
print('Removed Element:', removed_element)
print('Updated List:', nested_list)

Output:

Removed Element: date
Updated List: [['apple', 'banana'], ['cherry']]

In this example, we have a nested list of fruits, and we want to remove the second element of the second list, which is ‘date’. We use the indexing notation to access the second list, and then call the pop() method with the index value of the element to be removed, which is 1. It removes and returns the element ‘date’, which we store in a variable called removed_element. Then we print the updated nested list, which has the second element of the second list removed.

Example 5: Using pop() with Negative Index Values

We can also use pop() with negative index values, which count from the end of the list. Here’s an example:

fruits = ['apple', 'banana', 'cherry', 'date']
second_last_fruit = fruits.pop(-2)
print('Removed Fruit:', second_last_fruit)
print('Updated List:', fruits)

Output:

Removed Fruit: cherry
Updated List: ['apple', 'banana', 'date']

In this example, we have a list of fruits, and we want to remove the second last fruit from the list, which has a negative index value of -2. We call the pop() method with the negative index value of the element to be removed, which is -2. It removes and returns the element ‘cherry’, which we store in a variable called second_last_fruit. Then we print the updated list, which has the second last element removed.

Conclusion

In this article, we explored the pop() method in Python, which is used to remove and return an element from a list. We saw how it can be used to remove the last element, remove an element by index, remove multiple elements, remove elements from a nested list, and use negative index values. We also learned related concepts like indexing and looping that can be useful when working with lists in Python.