Table of Contents
In this tutorial, we will learn about the Python List pop() method with the help of examples.
The pop()
method removes the item at the given index from the list and returns the removed item.
Example
# create a list of prime numbers prime_numbers = [2, 3, 5, 7] # remove the element at index 2 removed_element = prime_numbers.pop(2) print('Removed Element:', removed_element) # Output: Removed Element: 5
1. Syntax of List pop()
The syntax of the pop()
method is:
list.pop(index)
2. pop() parameters
- The
pop()
method takes a single argument (index). - The argument passed to the method is optional. If not passed, the default index -1 is passed as an argument (index of the last item).
- If the index passed to the method is not in range, it throws IndexError: pop index out of range exception.
3. Return Value from pop()
The pop()
method returns the item present at the given index. This item is also removed from the list.
4. Example 1: Pop item at the given index from the list
# programming languages list languages = ['Python', 'Java', 'C++', 'French', 'C'] # remove and return the 4th item return_value = languages.pop(3) print('Return Value:', return_value) # Updated List print('Updated List:', languages)
Output
Return Value: French Updated List: ['Python', 'Java', 'C++', 'C']
Note: Index in Python starts from 0, not 1.
If you need to pop the 4th element, you need to pass 3 to the pop()
method.
5. Example 2: pop() without an index, and for negative indices
# programming languages list languages = ['Python', 'Java', 'C++', 'Ruby', 'C'] # remove and return the last item print('When index is not passed:') print('Return Value:', languages.pop()) print('Updated List:', languages) # remove and return the last item print('\nWhen -1 is passed:') print('Return Value:', languages.pop(-1)) print('Updated List:', languages) # remove and return the third last item print('\nWhen -3 is passed:') print('Return Value:', languages.pop(-3)) print('Updated List:', languages)
Output
When index is not passed: Return Value: C Updated List: ['Python', 'Java', 'C++', 'Ruby'] When -1 is passed: Return Value: Ruby Updated List: ['Python', 'Java', 'C++'] When -3 is passed: Return Value: Python Updated List: ['Java', 'C++']
If you need to remove the given item from the list, you can use the remove() method.
And, you can use the del
statement to remove an item or slices from the list.
Related posts:
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python String expandtabs()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python dir()
Python Program to Reverse a Number
Python String isnumeric()
Python Matrices and NumPy Arrays
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Add Two Matrices
Python Program to Differentiate Between del, remove, and pop on a List
Python Program to Find HCF or GCD
Python List Comprehension
Using a List of Values in a JdbcTemplate IN Clause
Python max()
Python eval()
Python print()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python String zfill()
Python Dictionary fromkeys()
Python pow()
Python super()
Deep Learning with Python - Francois Cholletf
Python Program to Differentiate Between type() and isinstance()
Python datetime
Python Tuple
Python Program to Print Colored Text to the Terminal
Python Dictionary keys()
Python int()
Python enumerate()
Python dict()
Python Namespace and Scope
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper