Just to answer part of the question: popping from the end (the right end) of a list takes constant time in CPython, but popping from the left end (.pop(0)) takes time proportional to the length of the list: all the elements in the_list[1:] are physically moved one position to the left.

If you need to delete index position 0 frequently, much better to use an instance of collections.deque. Deques support efficient pushing and popping from both ends.

BTW, when I run the program, I get a clean exception:

...
length of pmarbs = 8306108
Traceback (most recent call last):
  File "xxx.py", line 22, in <module>
    pmarbs.append(pot2)
MemoryError

That happened to be on a 32-bit Windows box. And it doesn't surprise me ;-)

Answer from Tim Peters on Stack Overflow
Top answer
1 of 2
24

Just to answer part of the question: popping from the end (the right end) of a list takes constant time in CPython, but popping from the left end (.pop(0)) takes time proportional to the length of the list: all the elements in the_list[1:] are physically moved one position to the left.

If you need to delete index position 0 frequently, much better to use an instance of collections.deque. Deques support efficient pushing and popping from both ends.

BTW, when I run the program, I get a clean exception:

...
length of pmarbs = 8306108
Traceback (most recent call last):
  File "xxx.py", line 22, in <module>
    pmarbs.append(pot2)
MemoryError

That happened to be on a 32-bit Windows box. And it doesn't surprise me ;-)

2 of 2
12

list.pop(index) is an O(n) operation, because after you remove the value from the list, you have to shift the memory location of every other value in the list over one. Calling pop repeatedly on large lists is great way to waste computing cycles. If you absolutely must remove from the front of a large list over and over use collections.deque, which will give you much faster insertions and deletions to thr front.

len() is O(1) because deletions are O(n), since if you make sure all the values in a list are allocated in memory right next to each other, the total length of a list is just the tail's memory location - the head's memory location. If you don't care about the performance of len() and similar operations, then you can use a linked list to do constant time insertions and deletions - that just makes len() be O(n) and pop() be O(1) (and you get some other funky stuff like O(n) lookups).

Everything I said about pop() goes for insert() also - except for append(), which usually takes O(1).

I recently worked on a problem that required deleting lots of elements from a very large list (around 10,000,000 integers) and my initial dumb implementation just used pop() every time I needed to delete something - that turned out to not work at all, because it took O(n) to do even one cycle of the algorithm, which itself needed to n times.

My solution was to create a set() called ignore in which I kept the indices of all "deleted" elements. I had little helper functions to help me not have to think about skipping these, so my algorithm didn't get too ugly. What eventually did it was doing a single O(n) pass every 10,000 iterations to delete all the elements in ignore and make ignore empty again, that way I got the increased performance from a shrinking list while only having to do one 10,000th of the work for my deletions.

Also, ya, you should get a memory error because you are trying to allocate a list that is definitely much larger than your hard drive - much less your memory.

🌐
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 …
Discussions

I was surprised at how slow list.pop() is! And list.remove() is even many times slower
This is simply how lists work, nothing surprising here. Removing the first element requires moving all the elements after it one step to the left to fill that gap, which makes this operation run in linear time. It means that clearing the list this way is O(n2), so it unsurprisingly takes a long time, as bubble sorting the (shuffled) list could even be faster. This is why we think of alternatives when solving problems, such as using collections.deque, reversing the list (O(n) instead of O(n2)) or just using pop() from the end if it works. More on reddit.com
🌐 r/learnpython
38
57
August 22, 2021
What is the most efficient way to push and pop a list in Python? - Stack Overflow
In Python how do I write code which shifts off the last element of a list and adds a new one to the beginning - to run as fast as possible at execution? There are good solutions involving the use of More on stackoverflow.com
🌐 stackoverflow.com
What is the time complexity of popping elements from list in Python? - Stack Overflow
I wonder what the time complexity of the pop method of list objects is in Python (in CPython particulary). Also does the value of N for list.pop(N) affect the complexity? More on stackoverflow.com
🌐 stackoverflow.com
performance - Python list pop() much slower than list[1:] - Stack Overflow
Just a small point but that answer is technically measuring different things. pop without parameters defaults to last element. But the task it to remove first element. The answer is still pop is faster, but the numbers are different. (docs.python.org/2/library/stdtypes.html) 2016-02-26T10:... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Runestone Academy
runestone.academy › ns › books › published › pythonds3 › AlgorithmAnalysis › Lists.html
2.6. Lists — Problem Solving with Algorithms and Data Structures 3rd edition
Now that we have seen how performance ... Big O efficiency of all the basic list operations. After thinking carefully about Table 2, you may be wondering about the two different times for pop. When pop is called on the end of the list it takes \(O(1)\), but when pop is called on the first element in the list—or anywhere in the middle it—is \(O(n)\) The reason for this lies in how Python chooses to ...
🌐
Analytics Vidhya
analyticsvidhya.com › home › understanding python pop() method
Understanding Python pop() Method
October 12, 2024 - When working with large lists, frequent use of pop() (especially from indices other than the last one) can cause performance degradation because of the repeated need to shift elements. However, removing elements from the end of the list (using pop() with no index) is efficient and doesn’t involve shifting.
🌐
Reddit
reddit.com › r/learnpython › i was surprised at how slow list.pop() is! and list.remove() is even many times slower
r/learnpython on Reddit: I was surprised at how slow list.pop() is! And list.remove() is even many times slower
August 22, 2021 -

I know there is a list.clear(), I'm just sharing that I didn't expect that using list.pop() and list.remove() specifically could slow down the program that much.

li = list(range(500000))

Creating a list is quick.

So we are going to test out pop/remove specific values. For the purpose of this "benchmark", we are going to remove all elements from the list:

while (li):
    li.pop(0)

It took 74.735 seconds to pop all the elements! It's ridiculously long.
I KNOW it would have been much faster if I even had used li.pop() without the index or maybe used filter function, list comprehension with conditional or whatever
But that's what I'm trying to show, how slow it is to remove certain list items specifically using pop and remove methods.

And li.remove(), which always requires a specified value to remove, is even worse than pop!

 for num in li:
    li.remove(num)

This one took me 303.268 seconds to complete. How crazy it is.

I've been having fun with abstract data structures. Implemented linked lists and a queues running on linked lists.

And for the sake of interest, I decided to compare the performance of the queue based on the linked list and the usual python list. And I was surprised. When my linked list Queue dequeued 500.000 elements in 0.5 seconds, while python list Queue was doing it in 75 seconds.

Top answer
1 of 5
46
This is simply how lists work, nothing surprising here. Removing the first element requires moving all the elements after it one step to the left to fill that gap, which makes this operation run in linear time. It means that clearing the list this way is O(n2), so it unsurprisingly takes a long time, as bubble sorting the (shuffled) list could even be faster. This is why we think of alternatives when solving problems, such as using collections.deque, reversing the list (O(n) instead of O(n2)) or just using pop() from the end if it works.
2 of 5
13
Just thought I'd mention that, on top of being the slowest option presented here, for num in li: li.remove(num) is also broken; it skips every other value in the list and the result is essentially only half of the original list, not an empty one. The reason for this already came up in the other answers, as the values shift in the list when you remove one, but the loop itself doesn't take this into account. You can think of the loop as if it had a hidden index variable it updates on every iteration, and when the values are shifted what was previously going to be the next value after deletion is now where the deleted one was, the loop index goes up by one, and the next index to be removed is the one next to the current one. EDIT: It's easier to understand visually, I guess. idx | 0 | 1 | 2 | 3 | 4 | val | 1 | 2 | 3 | 4 | 5 Loop index: 0 Removing item at index 0 idx | 0 | 1 | 2 | 3 | 4 | val | 2 | 3 | 4 | 5 | ... Loop index: 1 Removing item at index 1 idx | 0 | 1 | 2 | 3 | 4 | val | 2 | 4 | 5 | ... | ... Loop index: 2 Removing item at index 2 Idx | 0 | 1 | 2 | 3 | 4 | val | 2 | 4 | ... | ... | ... EDIT #2: If you needed to empty a list in a real project, the best options would be to either reassign an empty list, or use list.clear which is way faster than using list.pop in a loop.
🌐
Bradfield CS
bradfieldcs.com › algos › analysis › performance-of-python-types
Performance of Python Types
When pop is called from the end, the operation is ... O(n)O(n). Why the difference? When an item is taken from the front of a Python list, all other elements in the list are shifted one position closer to the beginning.
🌐
Python
python-list.python.narkive.com › UtpemKrm › are-there-performance-concerns-with-popping-from-front-of-long-lists-vs-the-end-of-long-lists
Are there performance concerns with popping from front of long lists vs. the end of long lists?
When an item is popped from a list, all of the later items (they are actually references to each item) are moved down. Therefore, popping the last item is fast, but popping the first item is slow. If you want to pop efficiently from both ends, then a deque is the correct choice of container.
🌐
Python
wiki.python.org › moin › TimeComplexity
TimeComplexity - Python Wiki
[2] = Popping the intermediate element at index k from a list of size n shifts all elements after k by one slot to the left using memmove. n - k elements have to be moved, so the operation is O(n - k). The best case is popping the second to last element, which necessitates one move, the worst case is popping the first element, which involves n - 1 moves.
Find elsewhere
🌐
Esaezgil
esaezgil.com › home › python lists pop vs slice performance
Python lists: pop vs slice performance - Enrique Saez
February 22, 2017 - shows that slicing the list carries a performance penalty of ~50% compared to just doing a pop of the first element.
🌐
Narkive
tutor.python.narkive.com › yVyd0Dpe › why-is-list-pop-0-slow
[Tutor] why is list.pop(0) slow?
Hi Wari, By the way, if the order of your elements is not important, then there's a cute trick we can employ to remove elements from the front of a list: we can swap the first and last elements, and then do a pop() off then end of our list, because popping at the end is quite efficient.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › pop() in python
pop() in Python | Efficient Data Removal with Examples
May 13, 2024 - The pop() method in Python removes and returns an element from a list, using the specified index. Efficient list manipulation
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Codecademy
codecademy.com › docs › python › lists › .pop()
Python | Lists | .pop() | Codecademy
May 26, 2025 - Yes, .pop() is generally more efficient than other methods like .remove() when you know the index of the element to remove.
🌐
Progressive Robot
progressiverobot.com › home › python › how to use `.pop()` in python lists and dictionaries
Pop: Complete Guide - Progressive Robot
September 30, 2022 - Internally, this performs a hash-table ... Efficiency: The .pop() method is highly efficient because it combines two actions, removing and retrieving an item into a single, atomic operation....
Price: $$
Address: Chester Business Park, 220 Heronsway, CH4 9GB
🌐
DataCamp
datacamp.com › tutorial › python-pop
How to Use the Python pop() Method | DataCamp
July 31, 2024 - When we use the pop() method to ... to learn more about time complexity in Python. The pop() method in dictionaries is highly efficient due to the implementation of the underlying hash table....
🌐
Quora
quora.com › What-is-the-time-complexity-of-the-pop-function-in-a-Python-list
What is the time complexity of the pop() function in a Python list? - Quora
Answer: Depends upon whether you pop from the end (which is the default when you pass no argument), or pop a specific position (which you can do, by passing an index number). Pop from the end is O(1) of course, but popping a specific position is O(n) because the list elements are then shifted to ...
🌐
Mimo
mimo.org › glossary › python › pop()
Python Pop Method: Essential Data Manipulation techniques
Defaults to the Last Item: If you call .pop() with no index, it will remove and return the last item in the list, which is very efficient. Removes by Index: When you provide an integer argument, .pop() removes the item at that specific index. Raises an IndexError: If you try to .pop() from ...
🌐
TheLinuxCode
thelinuxcode.com › home › a thorough guide to the pop() function in python
A Thorough Guide to the Pop() Function in Python – TheLinuxCode
December 20, 2024 - When pop() hits negatives, reallocation shrinks. Release then provides reuse efficiency. And there we have it – a rare full stack comprehension from interface down to internals! ... We covered everything from basic syntax to internals arcana! Whether just starting out or possessing mastery, solidifying knowledge of Python builtin functions ensures skill growth for all.