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
The pop() method removes the element at the specified position. ... Note: The pop() method returns removed value. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_dictionary_pop.asp
Python Dictionary pop() Method
Python Examples Python Compiler ... } car.pop("model") print(car) Try it Yourself ยป ยท The pop() method removes the specified item from the dictionary....
๐ŸŒ
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).
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_set_pop.asp
Python Set pop() Method
The pop() method removes a random item from the set. This method returns the removed item. ... No parameter values. ... Note: The pop() method returns removed value. ... If you want to use W3Schools services as an educational institution, team ...
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_dsa_stacks.asp
Stacks with Python
While Python lists can be used as stacks, creating a dedicated Stack class provides better encapsulation and additional functionality: ... class Stack: def __init__(self): self.stack = [] def push(self, element): self.stack.append(element) def pop(self): if self.isEmpty(): return "Stack is empty" return self.stack.pop() def peek(self): if self.isEmpty(): return "Stack is empty" return self.stack[-1] def isEmpty(self): return len(self.stack) == 0 def size(self): return len(self.stack) # Create a stack myStack = Stack() myStack.push('A') myStack.push('B') myStack.push('C') print("Stack: ", myStack.stack) print("Pop: ", myStack.pop()) print("Stack after Pop: ", myStack.stack) print("Peek: ", myStack.peek()) print("isEmpty: ", myStack.isEmpty()) print("Size: ", myStack.size()) Run 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.)

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]
๐ŸŒ
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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-list-pop-method
Python List pop() Method - GeeksforGeeks
October 24, 2024 - For example, consider a list l ... to find the length of ... The pop() method is used to remove an element from a list at a specified index and return that element....
๐ŸŒ
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.
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ python pop() function | list & dictionaries (+code examples)
Python pop() Function | List & Dictionaries (+Code Examples)
November 11, 2024 - When dealing with negative indices, Python interprets them as counting from the end of the list rather than the beginning. This can be particularly useful when you want to remove elements from the end without having to calculate the actual index. ... # List of elements my_list = [10, 20, 30, 40, 50] # Using pop() with a negative index to remove the last item last_item = my_list.pop(-1) # Displaying the removed item and updated list print(last_item) print(my_list)
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.6 documentation
It raises a ValueError if there is no such item. ... 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.
๐ŸŒ
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.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ pop-python
How to Use `.pop()` in Python Lists and Dictionaries | DigitalOcean
July 24, 2025 - Learn how to use the .pop() method in Python to remove items from lists and dictionaries. Includes syntax, examples, differences from remove(), edge caseโ€ฆ
๐ŸŒ
W3Schools
w3schools.com โ€บ dsa โ€บ dsa_data_stacks.php
W3Schools.com
Python: class Node: def __init__(self, value): self.value = value self.next = None class Stack: def __init__(self): self.head = None self.size = 0 def push(self, value): new_node = Node(value) if self.head: new_node.next = self.head self.head = new_node self.size += 1 def pop(self): if self.isEmpty(): return "Stack is empty" popped_node = self.head self.head = self.head.next self.size -= 1 return popped_node.value def peek(self): if self.isEmpty(): return "Stack is empty" return self.head.value def isEmpty(self): return self.size == 0 def stackSize(self): return self.size myStack = Stack() myStack.push('A') myStack.push('B') myStack.push('C') print("Pop: ", myStack.pop()) print("Peek: ", myStack.peek()) print("isEmpty: ", myStack.isEmpty()) print("Size: ", myStack.stackSize()) Try it Yourself ยป ยท
๐ŸŒ
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 Python pop() method allows you to remove an element from a specified position in a list. This method is useful if you have an existing array that contains a value you want to remove.
๐ŸŒ
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.