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 Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python 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()?
Why do we take the elements from ...toriented-python/hacknslash/game-planning), and don't use pop() method with no arguments? In this case, we would take items from the list from the end and speed would be always equal to O(1), and if you take elements from the beginning of the list, the performance will be O(n) because we have to move all elements of a list one step to the left. In this example, it will not ... 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 - How does CPython implement pop(0)? - Stack Overflow
pop(0) removes the left hand element from L. I don't know how CPython does this under the hood and keeps the same id. But we can see from timings that it is doing a lot more work than pop() does. For example: More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
Mimo
mimo.org › glossary › python › pop()
Python Pop Method: Essential Data Manipulation techniques
In Python, pop() is also ideal for scenarios where you need iterate through a list while removing items. For example, consider task scheduling or processing a queue of actions. ... tasks = ["write", "debug", "test"] while tasks: current_task = tasks.pop(0) # Remove and return the first task ...
🌐
Codecademy
codecademy.com › docs › python › lists › .pop()
Python | Lists | .pop() | Codecademy
May 26, 2025 - This example shows how to remove ... parameter: ... In this example, the element at index 2 (“Orange”) is removed first, followed by the removal of the element at index 0 (“Apple”)....
🌐
Finxter
blog.finxter.com › home › learn python blog › python list pop()
Python List pop() – Be on the Right Side of Change
June 19, 2021 - 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) for i in range(n)] to remove and return the first n elements of the list.
Find elsewhere
🌐
Unstop
unstop.com › home › blog › python pop() function | list & dictionaries (+code examples)
Python pop() Function | List & Dictionaries (+Code Examples)
November 11, 2024 - Next, we use a try block to attempt removing an element at index 5 using pop(5). However, since index 5 is out of range for the list (which only has indices 0, 1, and 2), this results in an IndexError. The except IndexError block catches the error, and we print the message "Error: Index out of range!" to inform us about the issue. Finally, we print the updated my_list, which remains unchanged as [10, 20, 30] because the pop() operation was not successful. In Python, the pop() method allows you to remove and return an element from a list, either by specifying its index or using the default behavior of removing the last element.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-pop-method
Python List pop() Method - GeeksforGeeks
September 11, 2025 - Return Value: Returns the removed element and raises IndexError if the specified index is out of range. Example 1: In this example, an index is provided to pop() so that an element can be removed from a particular position in the list.
🌐
DigitalOcean
digitalocean.com › community › tutorials › pop-python
How to Use `.pop()` in Python Lists and Dictionaries | DigitalOcean
July 24, 2025 - Understanding the difference between .pop() and .remove() is essential when working with lists in Python, as they serve distinct purposes and have different behaviors. ... You know the index (for lists) or key (for dictionaries) of the element you want to remove. You need to both delete and retrieve the element. You’re working with stacks or implementing data-processing routines where you consume and discard data progressively. # List example data = [10, 20, 30] value = data.pop(1) # Removes and returns 20 print(data) # Output: [10, 30] # Dictionary example config = {'debug': True, 'port': 8000} port = config.pop('port', 8080) # Removes and returns 8000
🌐
Programiz
programiz.com › python-programming › methods › list › pop
Python List pop()
# 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.
🌐
Simplilearn
simplilearn.com › home › resources › software development › pop in python: an introduction to pop function with examples
Pop in Python: An Introduction to Pop Function with Examples
November 13, 2025 - Pop in Python is a pre-defined, in-built function. Learn pop function's ✓ syntax ✓ parameters ✓ examples, and much more in this tutorial. Start learning now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
DataCamp
datacamp.com › tutorial › python-pop
How to Use the Python pop() Method | DataCamp
July 31, 2024 - You can also use a positive or negative index as an argument in the pop() method to specify the element to remove from the list. In Python, positive indices start from 0 at the beginning of the list. Negative indices begin from -1 at the end of the list. The following examples show these three methods for removing elements from a list.
🌐
Narkive
tutor.python.narkive.com › yVyd0Dpe › why-is-list-pop-0-slow
[Tutor] why is list.pop(0) slow?
Permalink I've been playing around with very large lists and found out by chance that pop(0) is 8 times slower than pop(). Is it really that bad? Performance improved when the list i have is reversed and used pop(). See this little snippet of code for example: --- """ This shows that poping from the back of the list is faster then the beginning of the list """ import profile def generate(num = 2000): return [[] for p in xrange(num)] def popper(l): while l: l.pop() def pooper(l): while l: l.pop(0) if __name__ == '__main__': print "###### Time taken to generate list" profile.run('generate(50000)
🌐
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 - Pop first element of a queue in Python — list.pop(0) vs deque.popleft() The time complexity of deque.popleft() is O(1), while the time complexity of list.pop(0) is O(k), as index 0 is considered an …
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.

🌐
Codecademy Forums
discuss.codecademy.com › data science
[Beginner] What's the difference between list = list[1:] and list.pop(0)? Should lead to the same result, right? - Data Science - Codecademy Forums
December 10, 2023 - This question comes from the exercise “Delete Starting Even Numbers” in the Python 3 course for beginners, section Functions. The suggested solution uses list = list[1:] whereas I was attempting it with list.pop(0). Since both functions remove the element in the 0th index of the list, I ...