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
🌐
W3Schools
w3schools.com › python › ref_list_pop.asp
Python List pop() Method
Remove List Duplicates Reverse ... Study Plan Python Interview Q&A Python Training ... The pop() method removes the element at the specified position....
Discussions

stack - Python pop() vs pop(0) - Stack Overflow
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. ... Save this answer. ... Show activity on this post. 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... More on stackoverflow.com
🌐 stackoverflow.com
[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
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
python - Pop multiple items from the beginning and end of a list - Stack Overflow
Suppose I have a list of items like this: mylist=['a','b','c','d','e','f','g','h','i'] I want to pop two items from the left (i.e. a and b) and two items from the right (i.e. h,i). I want the most More on stackoverflow.com
🌐 stackoverflow.com
People also ask

What does list.pop() do in Python?
It removes and returns the item at an index, defaulting to the last item when no index is supplied.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python list.pop(): remove and return items safely
Python list.pop(): Remove and Return Items Safely
Does pop() mutate the original list?
Yes. The list becomes shorter and the removed value is returned to the caller.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python list.pop(): remove and return items safely
Python list.pop(): Remove and Return Items Safely
Can list.pop() remove the first item?
Yes, pop(0) removes and returns the first item, but deque.popleft() is generally a better fit for repeated front removals.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python list.pop(): remove and return items safely
Python list.pop(): Remove and Return Items Safely
🌐
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 - How slow is python list.pop(0) ? An empirical study on python list.pop complexity TL;DR Python list.pop(k) has a time complexity of O(n). Be cautious when use a python list as a Queue structure. Use …
🌐
Finxter
blog.finxter.com › home › learn python blog › python list pop()
Python List pop() – Be on the Right Side of Change
June 19, 2021 - This will pop the first element from the list. ... The pop(0) method removes the first element 1 from the list of prime numbers given in the example.
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.)

🌐
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.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-pop-method
Python List pop() Method - GeeksforGeeks
July 17, 2026 - Explanation: a.pop() removes and returns the last element of the list and removed value is stored in val.
🌐
Python Pool
pythonpool.com › home › tutorials › python list.pop(): remove and return items safely
Python list.pop(): Remove and Return Items Safely
July 13, 2026 - For deeper indexing rules and boundary mistakes, see PythonPool’s list index out of range guide. pop(0) removes the first item, but it shifts the remaining items left.
🌐
Programiz
programiz.com › python-programming › methods › list › pop
Python List pop() (with Code Visualization)
Online Python Online JavaScript Online SQL Online Java Online HTML Online C Online C++ Online C# Online PHP Online Swift Online Kotlin Online TypeScript Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The list pop() method removes and returns the item at a specified index.
🌐
Medium
medium.com › @mollihua › pop-first-element-of-a-queue-in-python-list-pop-0-vs-collections-deque-popleft-7991408e45b
list.pop(0) vs deque.popleft() | by mollihua
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 …
🌐
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 ...
🌐
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).
🌐
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 ...
🌐
Python Guides
pythonguides.com › python-list-pop-method
Python pop() List Method
October 13, 2025 - In this code, I used pop(0) to remove the first element each time. This approach processes the list in FIFO (First In, First Out) order, which is useful for queue-like operations.
🌐
AskPython
askpython.com › python › list › python-list-pop
Python List pop() Method: Remove and Return by Index - AskPython
2 weeks ago - It removes whatever lives at that index and preserves the remaining order and i popped a string and a float from the same mixed list to verify that. Many bugs come from ignoring the return. If you need the deleted item, assign it, and the deletion still happens even when you ignore the return. queue = [3, 2, 1] total = 0 while queue: total += queue.pop() print(total)
🌐
Note.nkmk.me
note.nkmk.me › home › python
Remove an Item from a List in Python: remove, pop, clear, del | note.nkmk.me
April 17, 2025 - The in operator in Python (for list, string, dictionary, etc.) The pop() method removes the item at a given index and returns its value. Indexing is zero-based. l = [0, 10, 20, 30, 40, 50] popped_item = l.pop(0) print(popped_item) # 0 print(l) ...
🌐
Tutorialspoint
tutorialspoint.com › python › list_pop.htm
Python List pop() Method
The Python List pop() method removes and returns the last object from the list, by default. However, the method also accepts an optional index argument and the element at the index is removed from the list.
🌐
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.