The first test isn't surprising; three elements are removed off the end.

The second test is a bit surprising. Only two elements are removed. Why?

List iteration in Python essentially consists of an incrementing index into the list. When you delete an element you shift all the elements on the right over. This may cause the index to point to a different element.

Illustratively:

start of loop
[0,0,0,1,2,3,4,5,6]
 ^   <-- position of index

delete first element (since current element = 0)
[0,0,1,2,3,4,5,6]
 ^

next iteration
[0,0,1,2,3,4,5,6]
   ^

delete first element (since current element = 0)
[0,1,2,3,4,5,6]
   ^

and from now on no zeros are encountered, so no more elements are deleted.


To avoid confusion in the future, try not to modify lists while you're iterating over them. While Python won't complain (unlike dictionaries, which cannot be modified during iteration), it will result in weird and usually counterintuitive situations like this one.

Answer from nneonneo on Stack Overflow
Top answer
1 of 4
33

The first test isn't surprising; three elements are removed off the end.

The second test is a bit surprising. Only two elements are removed. Why?

List iteration in Python essentially consists of an incrementing index into the list. When you delete an element you shift all the elements on the right over. This may cause the index to point to a different element.

Illustratively:

start of loop
[0,0,0,1,2,3,4,5,6]
 ^   <-- position of index

delete first element (since current element = 0)
[0,0,1,2,3,4,5,6]
 ^

next iteration
[0,0,1,2,3,4,5,6]
   ^

delete first element (since current element = 0)
[0,1,2,3,4,5,6]
   ^

and from now on no zeros are encountered, so no more elements are deleted.


To avoid confusion in the future, try not to modify lists while you're iterating over them. While Python won't complain (unlike dictionaries, which cannot be modified during iteration), it will result in weird and usually counterintuitive situations like this one.

2 of 4
12

since in list or Stack works in last in first out[LIFO] so pop() is used it removes last element in your list

where as pop(0) means it removes the element in the index that is first element of the list

as per the Docs

list.pop([i]):

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)

🌐
W3Schools
w3schools.com › python › ref_list_pop.asp
Python List pop() Method
Python Examples Python Compiler ... Interview Q&A Python Bootcamp Python Training ... The pop() method removes the element at the specified position....
Discussions

Why do we use pop(0) instead of pop()?
Evgeny Panov is having issues with: Why do we take the elements from the start of the list ( More on teamtreehouse.com
🌐 teamtreehouse.com
1
October 7, 2016
I don't think I understand what the .pop() method does on a list....
You shouldn't modify the list while iterating over it. It leads to weirdness like this. More on reddit.com
🌐 r/learnpython
9
3
March 2, 2021
python - What is the difference between pop() and pop()[0]? - Stack Overflow
Either way, pop returns the item that was removed. If you put [0] after the call to pop, you'll get the first item of the item that was popped. More on stackoverflow.com
🌐 stackoverflow.com
python - How does CPython implement pop(0)? - Stack Overflow
Here's my guess: pop() removes rightmost list element by shortening the length of the list by 1. pop(0) removes leftmost element by shifting the rest of the elements one place left and then shortening the length of the list by 1. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Mimo
mimo.org › glossary › python › pop()
Python Pop Method: Essential Data Manipulation techniques
In Python, pop() is a list method that removes and returns an element of a list. With an argument, pop() removes and returns the item at the specified index (starting from 0).
🌐
DigitalOcean
digitalocean.com › community › tutorials › pop-python
How to Use `.pop()` in Python Lists and Dictionaries | DigitalOcean
July 24, 2025 - Python’s .pop() method is a powerful and flexible built-in function that allows you to remove and return elements from both lists and dictionaries. This method is especially useful in scenarios where you need to both extract and delete items in a single, efficient operation.
🌐
Finxter
blog.finxter.com › home › learn python blog › python list pop()
Python List pop() – Be on the Right Side of Change
June 19, 2021 - But you can do it indirectly with a simple list comprehension statement. Say, you want to pop the first n elements from a list. How do you do this? You simply create a list of values using list comprehension [list.pop(0...
Find elsewhere
🌐
Medium
medium.com › @shuangzizuobh2 › how-well-do-you-code-python-9bec36bbc322
How slow is python list.pop(0) ?. An empirical study on python list.pop… | by Hj | Medium
September 27, 2023 - Essentially, a Python list is implemented as an array of pointers. The list.pop(k) first removes and returns the element at k, and then moves all the elements beyond k one position up.
🌐
freeCodeCamp
freecodecamp.org › news › python-pop-how-to-pop-from-a-list-or-an-array-in-python
Python .pop() – How to Pop from a List or an Array in Python
March 1, 2022 - So, to remove the first item in a list, you specify an index of 0 as the parameter to the pop() method. And remember, pop() returns the item that has been removed. This enables you to store it in a variable, like you saw in the previous section.
🌐
Codecademy
codecademy.com › docs › python › lists › .pop()
Python | Lists | .pop() | Codecademy
May 26, 2025 - 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.
🌐
Google Sites
sites.google.com › site › pythonpasapas › methodes › list › list-pop
Mon Python pas à pas - list ( ).pop ( )
C'est à dire que le premier item de la list ( ) sera utilisé et sortie de la list ( ) avec la méthode .pop ( 0 ), puis replacé en fin de list ( ) avec la méthode .append ( item_sorti ) (du premier au dernier), ou que le dernier item de ...
🌐
Programiz
programiz.com › python-programming › methods › list › pop
Python List pop()
The pop() method returns the item present at the given index. This item is also removed from the list. # programming languages list languages = ['Python', 'Java', 'C++', 'French', 'C']
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-pop-method
Python List pop() Method - GeeksforGeeks
2 days ago - Explanation: a.pop() removes and returns the last element of the list and removed value is stored in val.
🌐
Medium
medium.com › @mollihua › pop-first-element-of-a-queue-in-python-list-pop-0-vs-collections-deque-popleft-7991408e45b
Pop first element of a queue in Python — list.pop(0) vs deque.popleft() | by mollihua | Medium
July 2, 2020 - def listpop(alist): ts = time.time() alist.pop(0) te = time.time() print("{:e}".format(te - ts))def dequepopleft(alist): q = collections.deque(alist) ts = time.time() q.popleft() te = time.time() print("{:e} seconds".format(te - ts))a = [x for x in range(10**6)]listpop(a) # output: 4.558802e-03 seconds dequepopleft(a) # output: 2.861023e-06 seconds · Python ·
🌐
Codecademy
codecademy.com › forum_questions › 50856c70e033eb0200006b08
1.4 Use remove not pop. (Just feed back and better hint alternative) | Codecademy
Both pop and remove take an index not a value. pop removes the next index not that index.(Which is actually the problem.) The instructions say to remove the first index(0) not the second(Which is what pop is doing when you pass 0 into the ...
🌐
Unstop
unstop.com › home › blog › python pop() function | list & dictionaries (+code examples)
Python pop() Function | List & Dictionaries (+Code Examples)
November 11, 2024 - Indices are zero-based, so 0 refers to the first element, 1 to the second, and so on. Negative indices can also be used to refer to elements from the end of the list, where -1 refers to the last element.
🌐
Narkive
tutor.python.narkive.com › yVyd0Dpe › why-is-list-pop-0-slow
[Tutor] why is list.pop(0) slow?
If you can tell us more details about what you're expecting to do with your lists (lots of deletes, lots of inserts, etc.), we can probably chat about efficiency issues on Python-Tutor. ... Post by Gus Tabares item = list[0] del list[0] return item It's just as slow as l.pop(0), and for the same reason.
Top answer
1 of 2
3

I don't know anything about C but want to share my insight and it'd be too long for a comment.

That's the pop implementation.

If pop arg is the last element in the list, it calls list_resize which doesn't seem to be too complicated.

    if (index == Py_SIZE(self) - 1) {
        status = list_resize(self, Py_SIZE(self) - 1);
        if (status >= 0)
            return v; /* and v now owns the reference the list had */
        else
            return NULL;

Otherwise list_ass_slice is called. It happens it has a comment in it

/* Because [X]DECREF can recursively invoke list operations on this list, we must postpone all [X]DECREF activity until after the list is back in its canonical shape. Therefore we must allocate an additional array, 'recycle', into which we temporarily copy the items that are deleted from the list. :-( */

I'd assume that's where the performance is lost, on this temporary allocation.

2 of 2
1

You're asking two separate questions.

  • Why is the list ID the same before and after popping? As @Naman Chikara has explained in the comments, this is because the ID of a mutable object doesn't change even if the object does.

  • Why is pop() much faster than pop(0)? The answer is here:

Here's my guess: pop() removes rightmost list element by shortening the length of the list by 1. pop(0) removes leftmost element by shifting the rest of the elements one place left and then shortening the length of the list by 1. It is the shifting that is taking a lot of time.

As in many languages, it's much easier to add/remove items from the right side of an array than from the left.

🌐
Career Karma
careerkarma.com › blog › python › python pop: a step-by-step guide
Python Pop: A Step-By-Step Guide | Career Karma
December 1, 2023 - The pop() method removes an element from a specified position in a list and returns the deleted item. In this tutorial, we are going to discuss the Python pop() method, how it is used, and what arguments the function accepts.