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

🌐
cppreference.com
en.cppreference.com › cpp › container › queue
std::queue - cppreference.com
The queue pushes the elements on the back of the underlying container and pops them from the front. ... #include <cassert> #include <iostream> #include <queue> int main() { std::queue<int> q; q.push(0); // back pushes 0 q.push(1); // q = 0 1 q.push(2); // q = 0 1 2 q.push(3); // q = 0 1 2 3 ...
🌐
Cplusplus
cplusplus.com › reference › queue › queue › pop
std::queue::pop
The element removed is the "oldest" element in the queue whose value can be retrieved by calling member queue::front. This calls the removed element's destructor. This member function effectively calls the member function pop_front of the underlying container object.
🌐
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 ·
🌐
University of Vermont
uvm.edu › ~cbcafier › cs1210 › book › 11_loops › stacks_and_queues.html
Stacks and queues – Clayton Cafiero
June 24, 2025 - With a queue, we enqueue from one end and dequeue from the other. Like a stack, we can use append to enqueue. The little twist is that instead of .pop() which would pop from the same end, we use .pop(0) to pop from the other end of the list, and voilà, we have a queue.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › queue-push-and-queue-pop-in-cpp-stl
queue push() and pop() in C++ STL - GeeksforGeeks
3 weeks ago - Explanation: The first inserted element (10) is removed using pop(), and the remaining elements are printed. The following table list the main differences between queue::push() and queue::pop():
Find elsewhere
🌐
W3Schools
w3schools.com › python › python_dsa_queues.asp
Queues with Python
queue = [] # Enqueue queue.append('A') queue.append('B') queue.append('C') print("Queue: ", queue) # Peek frontElement = queue[0] print("Peek: ", frontElement) # Dequeue poppedElement = queue.pop(0) print("Dequeue: ", poppedElement) print("Queue after Dequeue: ", queue) # isEmpty isEmpty = not bool(queue) print("isEmpty: ", isEmpty) # Size print("Size: ", len(queue)) Try it Yourself » ·
🌐
GeeksforGeeks
geeksforgeeks.org › python › queue-in-python
Queue in Python - GeeksforGeeks
May 29, 2026 - q = [] q.append('a') q.append('b') q.append('c') print("Initial queue:", q) print("Elements dequeued from queue:") print(q.pop(0)) print(q.pop(0)) print(q.pop(0)) print("Queue after removing elements:", q)
🌐
CodingNomads
codingnomads.com › python-301-use-a-list-in-stack-or-queue
Use a List in a Stack or Queue
In this example, you're using two ... the end of the collection. .pop(0), used with an argument of 0, allows you to remove the first element from the collection....
🌐
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. Here is a demo example of using Queue to level-order traverse (generate) a tree.
🌐
Educative
educative.io › answers › what-is-queuepop-in-cpp
What is queue.pop() in C++?
queue_name.pop() // where the queue_name is the name of the queue · This function does not require a parameter. This function returns the element that is available at the front of the queue and removes that element from the queue.
🌐
Google Groups
groups.google.com › g › comp.lang.python › c › kiHYf5N0iz8
list.pop(0) vs. collections.dequeue
> On Jan 22, 12:14 pm, Chris Rebert <c...@rebertia.com> wrote: >> On Fri, Jan 22, 2010 at 11:14 AM, Steve Howell <showel...@yahoo.com> wrote: >> > The v2.6.4 version of the tutorial says this: >> >> > ''' >> > It is also possible to use a list as a queue, where the first element >> > added is the first element retrieved (“first-in, first-out”); however, >> > lists are not efficient for this purpose. While appends and pops from >> > the end of list are fast, doing inserts or pops from the beginning of >> > a list is slow (because all of the other elements have to be shifted >> > by one).
🌐
TutorialsPoint
tutorialspoint.com › cpp_standard_library › cpp_queue_pop.htm
C++ Queue::pop() Function
The C++ std::queue::pop() function is used to remove the front element of a queue. The queue will follows a First-In-First-Out(FIFO) principle, means the first element added is the first to be removed.
🌐
Fiu
users.cis.fiu.edu › ~weiss › Deltoid › vcstl › queue_pop.htm
queue::pop
//Compiler options: /GX #include <queue> #include <iostream> int main() { std::queue<int> qi ; //Constructs an empty queue, uses deque as default container. int i ; std::queue<int>::allocator_type a1 = qi.get_allocator() ; std::cout << "call qi.empty()" << std::endl ; if (qi.empty()) { std::cout << "queue is empty" << std::endl ; } else { std::cout << "queue contains some elements" << std::endl ; } std::cout << "qi.size() = " << qi.size() << std::endl ; std::cout << "Push Values on qi = " ; for(i = 0; i < 10; i++) { std::cout << i << ", " ; qi.push(i) ; } std::cout << std::endl ; std::cout << "qi.size() = " << qi.size() << std::endl ; std::cout << "Pop Values from qi = " ; while (!qi.empty()) { std::cout << qi.front() << ", " ; qi.pop() ; } std::cout << std::endl ; return 0 ; }
🌐
PHP
php.net › manual › en › ds-queue.pop.php
PHP: Ds\Queue::pop - Manual
(PECL ds >= 1.0.0) Ds\Queue::pop — Removes and returns the value at the front of the queue · public function Ds\Queue::pop(): mixed · Removes and returns the value at the front of the queue.
🌐
Codecademy
codecademy.com › docs › c++ › queues › .pop()
C++ (C Plus Plus) | Queues | .pop() | Codecademy
December 21, 2022 - The .pop() method removes the element at the front of the queue.