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
[Beginner] What's the difference between list = list[1:] and list.pop(0)? Should lead to the same result, right?
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 ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
0
December 10, 2023
Is popleft() faster than pop(0) ?

Yes. list.pop(0) is O(n), and deque.popleft() is O(1).

More on reddit.com
🌐 r/learnpython
9
6
May 14, 2020
🌐
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.
🌐
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).
🌐
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.
🌐
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.
Find elsewhere
🌐
Hyperskill
hyperskill.org › university › python › pop-in-python
Pop() in Python
October 14, 2025 - original_list = [1, 2, 3, 4, 5] removed_elements = [] # Remove elements removed_elements.append(original_list.pop()) removed_elements.append(original_list.pop(0)) # Restore elements for element in reversed(removed_elements): original_list.append(element) print(original_list) # Output: [3, 4, 5, 1, 5]
🌐
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.
🌐
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) ... Note: Index in Python starts from 0, not 1.
🌐
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 ...
🌐
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 ...
🌐
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 ·
🌐
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.
🌐
DataCamp
datacamp.com › tutorial › python-pop
How to Use the Python pop() Method | DataCamp
July 31, 2024 - Learn how to use Python's pop() method to remove elements from lists and dictionaries. Learn to avoid common errors like IndexError and KeyError.
🌐
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