Python List pop() Method

The pop() method is a built-in method in Python that allows you to remove and return an item from the end of a list. It modifies the original list by removing the last element, and it returns the removed element. The method takes an optional index parameter, which specifies the position of the item to be removed. If the index parameter is not provided, the method removes and returns the last item in the list.


Syntax:
list.pop(pos)

Python List pop()

Following are some examples of how to use the pop() method in different ways:

Removing the last item (without using an index):

colors = ['red', 'green', 'blue'] last_color = colors.pop() print(last_color) # Output: 'blue' print(colors) # Output: ['red', 'green']

In the above example, the pop() method is called with no parameters, so it removes and returns the last item in the colors list, which is 'blue'.

Removing an item at a specified index:

numbers = [100, 200, 300, 400, 500] third_number = numbers.pop(2) print(third_number) # Output: 300 print(numbers) # Output: [100, 200, 400, 500]

In the above example, the pop() method is called with an index of 2, so it removes and returns the item at that position, which is 300.

Using negative index to remove an item from the end:

colors = ['red', 'green', 'blue'] last_color = colors.pop(-1) print(last_color) # Output: 'blue' print(colors) # Output: ['red', 'green']

In the above example, the pop() method is called with a negative index of -1, which is equivalent to calling it with no parameters. So it removes and returns the last item in the colors list, which is 'blue'.

Removing multiple items from a list:

letters = ['a', 'b', 'c', 'd', 'e'] first_two_letters = letters.pop(0), letters.pop(0) print(first_two_letters) # Output: ('a', 'b') print(letters) # Output: ['c', 'd', 'e']

In the above example, the pop() method is called twice with an index of 0, which removes and returns the first item in the letters list, and then the second item. The two removed items are returned as a tuple, which is assigned to the first_two_letters variable.

Python Dictionary pop()

The pop() method is a built-in method in Python that allows you to remove and return a key-value pair from a dictionary. It modifies the original dictionary by removing the specified key-value pair, and it returns the value associated with the removed key. The method takes a required key parameter, which specifies the key of the item to be removed. If the key parameter is not found in the dictionary, the method raises a KeyError.

Following are some examples of how to use the pop() method in different ways:

Removing an item from a dictionary:

person = {'name': 'Jack', 'age': 45, 'city': 'Paris'} age = person.pop('age') print(age) # Output: 45 print(person) # Output: {'name': 'Jack', 'city': 'Paris'}

In the above example, the pop() method is called with the 'age' key, so it removes and returns the value associated with that key, which is 45.

Removing an item from a dictionary and providing a default value:

person = {'name': 'Jack', 'city': 'Paris'} age = person.pop('age', 0) print(age) # Output: 0 print(person) # Output: {'name': 'Jack', 'city': 'Paris'}

In the above example, the pop() method is called with the 'age' key, but since the key is not found in the dictionary, it returns the default value of 0 instead of raising a KeyError.

Removing an item from a dictionary using the last key:

phone_book = {'Rose': '555-1234', 'Boby': '555-5678', 'Mills': '555-9012'} last_person = list(phone_book.keys())[-1] last_number = phone_book.pop(last_person) print(last_number) # Output: '555-9012' print(phone_book) # Output:{'Rose': '555-1234', 'Boby': '555-5678'}

In the above example, the keys() method is used to get a list of all the keys in the phone_book dictionary, which is then indexed with [-1] to get the last key. The pop() method is then called with the last key, which removes and returns the associated value.

Common Errors: Python pop() Method

Following are some common errors that may occur when using the Python pop() method:

IndexError:

This error occurs when you try to pop an item from an empty list.

my_list = [] item = my_list.pop() # Output: IndexError: pop from empty list

In the above case, my_list is empty, so there is no item to pop and an IndexError is raised.

KeyError:

This error occurs when you try to pop a key that does not exist in the dictionary.

my_dict = {'red': 1, 'green': 2, 'blue': 3} value = my_dict.pop('yellow') # Output: KeyError: 'yellow'

In the above case, 'yellow' is not a key in my_dict, so a KeyError is raised.

To avoid these errors, it's important to ensure that the key you are trying to pop exists in the dictionary, the list you are trying to pop from is not empty, and you pass the correct parameter type to the pop() method.

Python pop time complexity

In Python, the pop() method for lists has a time complexity of O(1), which means that it takes a constant amount of time to remove and return the last element of a list, regardless of the size of the list. This is because the implementation of lists in Python uses dynamic arrays, which allow for constant-time access to the last element of the list.

However, if you use pop() to remove an element from the middle of the list or from a specific index, the time complexity will be O(n), where n is the number of elements in the list. This is because all the elements after the removed element need to be shifted to fill in the gap left by the removed element, which takes linear time proportional to the number of elements in the list.

Therefore, it is generally more efficient to use pop() to remove the last element of a list, and to use other methods such as del or remove() to remove elements from the middle of the list or from a specific index, especially for large lists.

Python pop() Vs remove()

Python pop() removes and returns an element from a list based on its index, while remove() removes the first occurrence of a specified value from the list. pop() modifies the original list and returns the removed element, while remove() only modifies the original list.