Open In App

Python List pop() Method

Last Updated : 21 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python list pop() function removes elements at a specific index from the list.

Example

Python




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:

Python




# 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.

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.

Python




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.

Python




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:



Previous Article
Next Article

Similar Reads

List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
Some of the list methods are mentioned in set 1 below List Methods in Python | Set 1 (in, not in, len(), min(), max()…) More methods are discussed in this article. 1. del[a : b] :- This method deletes all the elements in range starting from index 'a' till 'b' mentioned in arguments. 2. pop() :- This method deletes the element at the position mentio
4 min read
IndexError: pop from Empty List in Python
The IndexError: pop from an empty list is a common issue in Python, occurring when an attempt is made to use the pop() method on a list that has no elements. This article explores the nature of this error, provides a clear example of its occurrence, and offers three practical solutions to handle it effectively. Here, we will see how to fix Pop From
3 min read
Python Set pop() Method
Python set pop() removes any random element from the set and returns the removed element. In this article, we will see about the Python set pop() method. Example Input: {9, 1, 0} Output: {9, 1} Explanation: By using set pop() method, a random element 0 is removed from the set and remaining set is returned.Python Set pop() SyntaxSyntax: set_obj.pop(
2 min read
Python Dictionary pop() Method
Python dictionary pop() method removes and returns the specified element from the dictionary. Example: C/C++ Code # inializing dictionary student student = {"rahul":7, "Aditya":1, "Shubham":4} # priting original dictionary print(student) # using dictionary pop suspended = student.pop("rahul") # checking key o
4 min read
Python | Pandas Dataframe.pop()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas Pop() method is common in most of the data structures but pop() method is a little bit different from the rest. In a stack, pop do
2 min read
Python | Pandas Series.pop()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.pop() function returns the item corresponding to the passed index label. It als
2 min read
Wand push() and pop() in Python
We can use ImageMagick's internal graphic context stack to manage different styles and operations in Wand. There are total four push functions for context stack. push()push_clip_path()push_defs()push_pattern() push() function is used to grow context stack and pop() is another function and used to restore stack to previous push. Syntax : # for push(
2 min read
What Is Difference Between Del, Remove and Pop on Python Lists?
In python del is a keyword and remove(), pop() are in-built methods. The purpose of these three are same but the behavior is different remove() method delete values or object from the list using value and del and pop() deletes values or object from the list using an index. del Keyword: The del keyword delete any variable, list of values from a list
3 min read
How to create a pop up message when a button is pressed in Python - Tkinter?
In this article, we will see how to create a button and how to show a popup message when a button is pressed in Python. Tkinter is a standard Python Package for creating GUI applications. Tkinter may be a set of wrappers that implement the Tk widgets as Python classes. Python, when combined with Tkinter, provides a quick and straightforward way to
2 min read
IndexError: pop from Empty Deque in Python
In Python, the IndexError: pop from an empty deque is an error that occurs when trying to use the pop() method on an empty deque. A deque (double-ended queue) is a versatile data structure that allows efficient addition and removal of elements from both ends. This article explores the causes of the error, provides examples of its occurrence, and of
4 min read
three90RightbarBannerImg