Python List pop() Method
Python list pop() function removes elements at a specific index from the list.
Example
fruits = ["apple", "mango", "cherry"]
fruits.pop()
print(fruits)
Output:
['apple', 'mango']Python List pop() Syntax
list_name.pop(index)Parameter
- index (optional) – The value at the index is popped out and removed. If the index is not given, then the last element is popped out and removed.
Return
Returns The last value or the given index value from the list.
Exception:pop() method raises IndexError when the index is out of range.
What is the List pop method()?
pop() function removes and returns the value at a specific index from a list. It is an inbuilt function of Python.
It can be used with and without parameters; without a parameter list pop() returns and removes the last value from the list by default, but when given an index value as a parameter, it only returns and removes the element at that index.
How to Use List pop() Method in Python?
You can easily use pop() function in Python. Using the list pop() method you can remove an element from the list. Let’s understand it with an example:
# Using list
fruits = ["apple","banana","cherry","carrot"]
# removing carrot from the list
fruits.pop()
# printing new list
print(fruits)
Output
['apple', 'banana', 'cherry']More List pop() Examples
Let’s see how to pop an item from a list with examples:
1. Pop last element from list
Following code Pops and removes the last element from the list in Python.
my_list = [1, 2, 3, 4]
print("Popped element:", my_list.pop())
print("List after pop():", my_list)
Output
Popped element: 4
List after pop(): [1, 2, 3]
2. Pop the Item at a Specific Index from the List
Pops and removes the 3rd index element from the list.
my_list = [1, 2, 3, 4, 5, 6]
# Pops and removes the 3th index
# element from the list
print(my_list.pop(3), my_list)
Output
4 [1, 2, 3, 5, 6]3. Pop element at a Negative Index from a List
Pops and removes element 5 from the list.
my_list = [1, 2, 3, 4, 5, 6]
poped_item = my_list.pop(-2)
print("New list", my_list)
print("Poped Item", poped_item)
Output
New list [1, 2, 3, 4, 6]
Poped Item 5
Note: List pop() time complexity = O(n)
In this article, we have covered the Python list pop() function which is used to remove elements from a list. The list pop() method is an important operation of a list.
Read More Python List Methods
Also Read:
- Python – Remove first element of list
- Python – Remove rear element from list
- Python | Remove given element from the list
Python List pop() Method – FAQs
What Does pop() Do in Python?
In Python, the
pop()method is used in different contexts with lists and dictionaries, performing slightly different functions:
- For Lists: The
pop()method removes and returns an element from the list at the specified position. If no index is specified,pop()removes and returns the last item in the list.- For Dictionaries: The
pop()method removes the specified key and returns the corresponding value. If the key is not found, a default value can be returned if specified, otherwise aKeyErroris raised.Example:
my_list = [1, 2, 3, 4, 5]
popped_item = my_list.pop() # Removes the last item
print(popped_item) # Output: 5
print(my_list) # Output: [1, 2, 3, 4]Example:
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.pop('b')
print(value) # Output: 2
print(my_dict) # Output: {'a': 1, 'c': 3}
What is the Difference Between pop() and remove() Methods of List?
The difference between
pop()andremove()methods in Python lists is how they delete items:
pop():
- Removes and returns the element at the specified index.
- If no index is provided, it removes and returns the last item of the list.
- Can be useful when you need the value of the removed element.
remove():
- Removes the first occurrence of a value without returning it.
- Does not work by index but by the value itself.
- If the value is not found, it raises a
ValueError.Example:
my_list = ['a', 'b', 'c', 'd']
my_list.pop(2) # 'c' is removed and returnedExample:
my_list = ['a', 'b', 'c', 'd']
my_list.remove('c') # 'c' is removed, nothing is returned
What is the Difference Between pop and index in Python List?
pop()andindex()are fundamentally different methods used with lists:
pop():
- Method for removing an item from the list by index and returns the item.
index():
- Method for finding the index of the first occurrence of a specified value within the list.
- Does not modify the list.
- Raises a
ValueErrorif the value is not found.Example:
my_list = ['a', 'b', 'c', 'd']
index_of_c = my_list.index('c') # Returns 2
my_list.pop(index_of_c) # Removes 'c' from the list
What Function Does the pop() Method in a Dictionary Do?
In dictionaries, the
pop()method is used to remove a specified key and return the corresponding value:
- Removes the key-value pair from the dictionary.
- If the key is not found and no default value is specified, it raises a
KeyError. If a default value is provided, it returns this default value instead of raising an error.Example:
my_dict = {'a': 1, 'b': 2, 'c': 3}
value = my_dict.pop('b', 'Not Found') # Removes 'b' and returns 2
print(my_dict) # Output: {'a': 1, 'c': 3}
value_not_found = my_dict.pop('z', 'Not Found') # Returns 'Not Found' since 'z' is not in the dictionary
print(value_not_found)These functionalities make
pop()a versatile method for both lists and dictionaries in Python, allowing for controlled removal of elements by index or key, respectively

