In this Python tutorial, we will discuss the Python List pop() method. It is an essential method of the list class that allows us to remove the item from the Python List.
List pop() method in Python
Below are the topics that we are doing to discuss in this article:
- Introduction to Python List pop() method
- Syntax of the pop() method
- Purpose and use cases of the pop() method
Python List pop() method
The pop()
method is an in-built list method in Python that removes and returns the last item in the Python list by default.
However, we can also provide an index as an argument to specify which item should be removed and returned. This method is useful when we want to retrieve a specific element from a Python list while simultaneously removing it.
Syntax:
list.pop(index=-1)
Parameters:
index
(optional): The index of the element to be removed and returned. If not provided, it defaults to -1, indicating the last element.
Return value:
- The
pop()
method returns the removed item from the Python list. If the Python list is empty or the index is out of range, it raises anIndexError
.
pop() method in Python List Examples
Let’s explore some examples of using the pop()
method.
Example#1 Removing and returning the last element of a list:
us_states = ["California", "New York", "Texas", "Florida"]
popped_state = us_states.pop()
print(popped_state)
print(us_states)
- In this example, we have a Python list called
us_states
containing four U.S. state names as strings. - We use the
pop()
method to remove the last element from the Python list (in this case, ‘Florida’) and store it in a variable calledpopped_state
. - When we print
popped_state
, we see the output ‘Florida’, which is the element that was removed from the Python list. Printingus_states
now shows the modified Python list without ‘Florida’.
Output:
Example#2 Removing and returning an element by index from a list
us_presidents = ["George Washington", "John Adams", "Thomas Jefferson", "James Madison"]
popped_president = us_presidents.pop(2)
print(popped_president)
print(us_presidents)
- In this example, we have a Python list called
us_presidents
containing four U.S. president names as strings. - We use the
pop()
method with the index parameter set to 2 to remove the element at index 2 (‘Thomas Jefferson’) and store it in a variable calledpopped_president
. - When we print
popped_president
, we see the output ‘Thomas Jefferson’, which is the element that was removed from the Python list. Printingus_presidents
now shows the modified Python list without ‘Thomas Jefferson’.
Output:
Example#3 Removing and returning elements from a list using a loop
us_car_brands = ["Ford", "Chevrolet", "Tesla", "Dodge"]
while us_car_brands:
popped_brand = us_car_brands.pop()
print("Removed brand:", popped_brand)
- In this example, we have a list of U.S. car brands. We use a
while
loop along with thepop()
method to iteratively remove and return all elements from the list until it is empty. - The output shows each removed car brand in reverse order.
Output:
Example#4 Using a conditional statement with the pop() method
us_cities_population = [("New York City", 8419000), ("Los Angeles", 3927000), ("Chicago", 2721000), ("Houston", 2296000)]
city_to_remove = "Chicago"
for i in range(len(us_cities_population)):
if us_cities_population[i][0] == city_to_remove:
removed_city = us_cities_population.pop(i)
break
print("Removed city:", removed_city)
print("Remaining cities:", us_cities_population)
- In this example, we have a Python list of tuples containing U.S. city names and their respective populations. We use a
for
loop to iterate through the list, searching for a specific city (‘Chicago’). - Once the city is found, we use the
pop()
method with the current index to remove and return the Python tuple containing the city’s name and population. The modified Python list contains the remaining city tuples.
Output:
Conclusion
The Python list pop()
method is a powerful and convenient way to remove and return elements from a Python list. By understanding its syntax, usage, and best practices through examples, we can effectively utilize this method in your Python programming projects.
You may also like to read the following articles:
- Python List count() method [With Examples]
- How to Append String to Beginning Of List Python
- How to Add Elements in List in Python Using For Loop
- How to convert a set to a list in Python
- Python List sort() method [With Examples]
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.