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

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
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
Difference between pop and remove
Also, pop returns that value that was removed, remove doesn’t. More on reddit.com
🌐 r/learnpython
10
3
June 29, 2022
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
🌐
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 ...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-pop-method
Python List pop() Method - GeeksforGeeks
5 days ago - Explanation: a.pop() removes and returns the last element of the list and removed value is stored in val. ... Parameter: index (optional) - Position of the element to remove. Defaults to -1 (last element). 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.
Find elsewhere
🌐
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.
🌐
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 - 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.
🌐
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
🌐
iO Flood
ioflood.com › blog › python-pop
Python Pop() Method: Complete Guide
February 6, 2024 - In this example, we first target the inner list at index 0 (nested_list[0]), and then use the pop method to remove the item at index 1. The pop method returns ‘banana’, which we store in the variable item.
🌐
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
🌐
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”)....
🌐
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 ...
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 · Queue · 0 followers ·
🌐
Hyperskill
hyperskill.org › university › python › pop-in-python
Pop() in Python
October 14, 2025 - Example · my_list = [1, 2, 3, 4, 5] print(my_list.pop()) # Output: 5 print(my_list) # Output: [1, 2, 3, 4] To restore the original list after using pop(), store removed elements in another list and re-insert them in the original list when needed. original_list = [1, 2, 3, 4, 5] removed_elements ...
🌐
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.
🌐
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 - In this case, we tried to remove the item at the index position 1 in the list “names”. But, the list only has one item, whose index position is 0. The “IndexError: pop index out of range” error is similar to the “list index out of range” error. The list index out of range error occurswill see if you try to access an item that does not exist in a list. Read our guide on the Python list index out of range error for more information.
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › python › python list pop() method: remove and return items
Python List pop() Method: pop by Index, Last Item, First Item, and Errors (2026)
January 9, 2024 - Positive or negative indices must still point at a real slot after Python normalizes negatives to len + index. Asking for index 10 on a three-item list fails the same way as an overly negative index: ... For length n, valid indices are 0 through n - 1, or -n through -1 inclusive. A small helper keeps callers safe when the index might be invalid: ... def safe_pop(seq, index=-1): try: return seq.pop(index) except IndexError: return None print(safe_pop([1], 5)) print(safe_pop([]))