๐ŸŒ
Codecademy
codecademy.com โ€บ forum_questions โ€บ 50856c70e033eb0200006b08
1.4 Use remove not pop. (Just feed back and better hint alternative) | Codecademy
The instructions say to remove the first index(0) not the second(Which is what pop is doing when you pass 0 into the parenthesis). ... No, youโ€™re wrong about this. I just tried it out on the same exercise. If you type in n.remove(0), youโ€™ll get an error message. This is because there is no entry called โ€˜0โ€™ in the list. If you type n.remove(1), you will remove the first number as this is โ€˜1โ€™. If you type n.remove(5), you will remove the 3rd number, not the 5th index. The opposite is true for pop.
๐ŸŒ
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.
Discussions

python - Issue with reversing list using list.pop() - Stack Overflow
0 (Python) Using pop in python to take first element print it and then put it at the bottom of the list More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How do I remove the first item from a list? - Stack Overflow
How do I remove the first item from a list? [0, 1, 2, 3] โ†’ [1, 2, 3] More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
How to reverse order to pop out Python 3.6.4 - Stack Overflow
Instead of it deleting the 'ALL' first, I would like the popitem to delete the 'CS' first and continue in that order from top to bottom. Here is my code: my_dictionary ={ 's02.001':'CS', 's02.00... More on stackoverflow.com
๐ŸŒ stackoverflow.com
May 5, 2020
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ what-is-difference-between-del-remove-and-pop-on-python-lists
Difference Between Del, Remove and Pop in Python Lists - GeeksforGeeks
July 23, 2025 - del is a keyword and remove(), and pop() are in-built methods in Python. The purpose of these three is the same but the behavior is different.
๐ŸŒ
The Teclado Blog
blog.teclado.com โ€บ python-lists-remove-vs-pop
Python lists: remove() vs pop() - The Teclado Blog
August 24, 2022 - Learn how and when to use the remove() and pop() methods to remove items from a Python list.
๐ŸŒ
Rustcodeweb
rustcodeweb.com โ€บ 2025 โ€บ 08 โ€บ opposite-of-append-in-python-list-pop-remove.html
What is the Opposite of append()? Using pop() and remove() in Python Lists | RUSTCODE
August 4, 2025 - In Python, append() is used to add items to the end of a list. But what if you want to do the oppositeโ€”remove items? Python provides two main methods for this: pop() and remove().
๐ŸŒ
Quora
quora.com โ€บ How-can-I-reverse-a-list-in-Python-using-pop-and-append
How to reverse a list in Python using .pop() and .append() - Quora
Answer (1 of 3): So pop() by default removes an item from the list (the default is to remove the last item, but there is an index parameter to enable us to pop any item), and append() always adds an item to the end of the list. Lets imagine we have this list : 1, 2, 3, 4 and we want to end up ...
Find elsewhere
๐ŸŒ
Python
docs.python.org โ€บ 3.1 โ€บ tutorial โ€บ datastructures.html
https://docs.python.org/3.1/tutorial/datastructure...
September 4, 2012 - There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assignment of an ...
Top answer
1 of 4
5

How about a simple reversal of string.

>>> x = 'abcd'
>>> x[::-1]
'dcba'
>>> 

On your code:

Never mutate the list on which you are iterating with. It can cause subtle errors.

>>> strList = [1, 2, 3, 4, 5]
>>> reverseCharList = []
>>> for someChar in strList:
...     print strList
...     reverseCharList.append(strList.pop())
...     print strList
... 
[1, 2, 3, 4, 5]   <-- Iteration 1
[1, 2, 3, 4]
[1, 2, 3, 4]      <-- Iteration 2
[1, 2, 3]
[1, 2, 3]         <-- Iteration 3
[1, 2]

See the following. Since you are using iterator (for .. in ..). You can see the iterator details directly and how mutating the list messes up with iterator.

>>> strList = [1, 2, 3, 4, 5]
>>> k = strList.__iter__()
>>> k.next()
1
>>> k.__length_hint__()   <--- Still 4 to go
4
>>> strList.pop()         <---- You pop an element
5
>>> k.__length_hint__()   <----- Now only 3 to go
3
>>> 
>>> k.next()
2
>>> k.__length_hint__()
2
2 of 4
5
for someChar in strList:
    reverseCharList.append(strList.pop())

Is essentially the same as:

i = 0
while i < len(strList):
    reverseCharList.append(strList.pop())
    i += 1

First iteration i is 0, len(strList) is 4, and you pop+append 'd'.

Second iteration i is 1, len(strList) is 3, and you pop+append 'c'.

Third iteration i is 2, len(strList) is 2, so the loop condition fails and you're done.

(This is really done with an iterator on the list, not a local variable 'i'. I've shown it this way for clarity.)

If you want to manipulate the sequence you're iterating over it's generally better to use a while loop. eg:

while strList:
    reverseCharList.append(strList.pop())
๐ŸŒ
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.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.6 documentation
There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or clear the entire ...
๐ŸŒ
Quora
quora.com โ€บ How-do-I-use-the-pop-method-in-Python-without-modifying-the-actual-list
How to use the pop() method in Python without modifying the actual list - Quora
Answer (1 of 3): If you want to treat the list as read-only, then donโ€™t use pop( ), use __getitem__( ). The latter will tell you whatโ€™s in the list without changing the list. Of course you neednโ€™t use __getitem__ directly, considered ugly, just use square brackets.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ odd behavior of list.pop() method and del funtion
r/learnpython on Reddit: Odd behavior of list.pop() method and del funtion
February 9, 2022 -

I've been strugling with a program implementation for some time now, and I just noticed a very wierd behavior of the pop method (pop is in my file, and del, as an alternative, returned the same result). Here is a minimun reproductible example:

words = (5*'Loren ipsun ').split()
print(words,'\n')
for i in range(10):
    words.pop(i)
    print(words)

This is the console output:

['Loren', 'ipsun', 'Loren', 'ipsun', 'Loren', 'ipsun', 'Loren', 'ipsun', 'Loren', 'ipsun'] 

['ipsun', 'Loren', 'ipsun', 'Loren', 'ipsun', 'Loren', 'ipsun', 'Loren', 'ipsun']
['ipsun', 'ipsun', 'Loren', 'ipsun', 'Loren', 'ipsun', 'Loren', 'ipsun']
['ipsun', 'ipsun', 'ipsun', 'Loren', 'ipsun', 'Loren', 'ipsun']
['ipsun', 'ipsun', 'ipsun', 'ipsun', 'Loren', 'ipsun']
['ipsun', 'ipsun', 'ipsun', 'ipsun', 'ipsun']
Traceback (most recent call last):

IndexError: pop index out of range

Could someone explain to me why just Loren words are being erased?

[Edit] Solution:

words = (5*'Loren ipsun ').split()
print(words,'\n')
for i in range(10):
    words.pop(0)
    print(words)
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ removing items from python lists: `del` vs `pop` vs `remove`
Removing items from Python lists: `del` vs `pop` vs `remove` | Sentry
2 weeks ago - Iโ€™ve seen a few different ways to remove an item from a list in Python: The keyword del (e.g. del mylist[2]). The method remove() (e.g. mylist.remove(2)). The method pop(). (e.g. mylist.pop(2)) What are the advantages and disadvantages of each and when should I use one rather than another?
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.)