.pop()
The .pop()
method in Python removes an element from a list at a specified index and returns that element. The .pop()
method directly modifies the original list by removing the element at the given position. If no index is provided, it removes and returns the last element by default.
Lists in Python are mutable sequences, and the .pop()
method provides an efficient way to manipulate these lists dynamically. This is particularly useful in scenarios such as implementing stacks (using .pop()
without an index) and queues (using .pop(0)
), managing task lists, or processing data collections where items need to be removed after processing.
Syntax
list_name.pop(index)
Parameters:
index
(optional): The position of the element to remove. Defaults to-1
(the last item) if not provided.
Return value:
- The
.pop()
method returns the removed item from the specified index. If the index is out of range, it raises anIndexError
.
Example 1: Basic Usage of .pop()
in Python Lists
This example demonstrates how to use the .pop()
method to remove the last element from a list:
# Creating a list of numbersnumbers = [10, 20, 30, 40, 50]# Removing the last element using pop()last_element = numbers.pop()# Displaying the removed element and updated listprint("Removed element:", last_element)print("Updated list:", numbers)
This example results in the following output:
Removed element: 50Updated list: [10, 20, 30, 40]
The .pop()
method removes the last element 50 from the list and returns it. The original list is modified to contain only the remaining elements.
Example 2: Removing Elements at Specific Positions
This example shows how to remove elements from specific positions in a list using the .pop()
method with an index parameter:
# Creating a list of fruitsfruits = ["Apple", "Banana", "Orange", "Mango", "Kiwi"]# Removing element at index 2 (the third element)removed_fruit = fruits.pop(2)print("Removed fruit:", removed_fruit)print("Updated fruit list:", fruits)# Removing the first element (index 0)first_fruit = fruits.pop(0)print("Removed first fruit:", first_fruit)print("Final fruit list:", fruits)
This example results in the following output:
Removed fruit: OrangeUpdated fruit list: ['Apple', 'Banana', 'Mango', 'Kiwi']Removed first fruit: AppleFinal fruit list: ['Banana', 'Mango', 'Kiwi']
In this example, the element at index 2 (“Orange”) is removed first, followed by the removal of the element at index 0 (“Apple”). After each operation, the list is updated, and the removed element is returned.
Codebyte Example: Implementing a Stack with .pop()
This example demonstrates how to implement a simple stack (Last-In-First-Out data structure) using the .pop()
method:
In this example, a stack is created using a list and three elements are added to it. Then, the .pop()
method is used without an index to remove and process the elements in Last-In-First-Out (LIFO) order, which is characteristic of a stack data structure.
Frequently Asked Questions
1. What happens if I try to pop from an empty list?
Attempting to pop from an empty list will raise an IndexError
. Here’s an example:
empty_list = []try:empty_list.pop()except IndexError as e:print(f"Error: {e}")
Output:
Error: pop from empty list
2. Is .pop()
more efficient than other methods for removing elements?
Yes, .pop()
is generally more efficient than other methods like .remove()
when you know the index of the element to remove. It has O(1)
time complexity when removing the last element (default) and O(n)
when removing from other positions due to the need to shift elements.
3. How does .pop()
differ from .remove()
?
.pop(index)
: Removes the element at the specified index and returns it. Raises anIndexError
if the list is empty or the index is out of range..remove(value)
: Finds the first occurrence of the specified value and removes it without returning anything. Raises aValueError
if the specified value is not found in the list.
4. Can I use negative indices with .pop()
?
Yes, you can use negative indices with .pop()
. For example, list.pop(-1)
removes the last item, list.pop(-2)
removes the second-to-last item, and so on.
5. Does .pop()
create a copy of the list?
No, .pop()
modifies the original list in-place and does not create a copy.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours