You can find a short collection of useful list functions here.
list.pop(index)
>>> l = ['a', 'b', 'c', 'd']
>>> l.pop(0)
'a'
>>> l
['b', 'c', 'd']
>>>
del list[index]
>>> l = ['a', 'b', 'c', 'd']
>>> del l[0]
>>> l
['b', 'c', 'd']
>>>
These both modify your original list.
Others have suggested using slicing:
- Copies the list
- Can return a subset
Also, if you are performing many pop(0), you should look at collections.deque
from collections import deque
>>> l = deque(['a', 'b', 'c', 'd'])
>>> l.popleft()
'a'
>>> l
deque(['b', 'c', 'd'])
- Provides higher performance popping from left end of the list
You can find a short collection of useful list functions here.
list.pop(index)
>>> l = ['a', 'b', 'c', 'd']
>>> l.pop(0)
'a'
>>> l
['b', 'c', 'd']
>>>
del list[index]
>>> l = ['a', 'b', 'c', 'd']
>>> del l[0]
>>> l
['b', 'c', 'd']
>>>
These both modify your original list.
Others have suggested using slicing:
- Copies the list
- Can return a subset
Also, if you are performing many pop(0), you should look at collections.deque
from collections import deque
>>> l = deque(['a', 'b', 'c', 'd'])
>>> l.popleft()
'a'
>>> l
deque(['b', 'c', 'd'])
- Provides higher performance popping from left end of the list
Slicing:
x = [0,1,2,3,4]
x = x[1:]
Which would actually return a subset of the original but not modify it.
Is shifting required to pop the front of a list in Python? - Stack Overflow
something's wrong with pop() function
I was surprised at how slow list.pop() is! And list.remove() is even many times slower
Why do we use pop(0) instead of pop()?
Videos
The documentation says you can specify an index
>>> stack = [1,2,3,4,5]
>>> stack.pop(0)
1
>>> stack
[2, 3, 4, 5]
>>>
There are two ways to achieve that:
You can use the
dequein python, which has apopleft()method:from collections import deque deque_list = deque([1,2,3,4,5]) deque_list.popleft() print deque_list
It will shows deque([2, 3, 4, 5]) in the output, and its usage is like the list.
And you can also use
pop(0)to pop from front:stack = [1,2,3,4,5] stack.pop(0) # 1
No shifting is required while doing a pop operation at the list -head right?
Think of a list as an array of references, where the first element of the list is always at array position zero. When you pop the first element of the list, you have to shift all the reference to the left by one position.
One could imagine alternative implementations, where popping the front of the list would be cheap (e.g. deque-style). I think we can trust the Python docs on this one, and assume that this is not how the built-in list class is implemented.
If you need efficient removals from the front of the container, use collections.deque:
Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.
Removing the first element usually involves moving all other elements so the former [1] element is at [0] after the removal. Of course a C-based implementation might just change a pointer so the array starts at the second element instead of the first, but then it would have to track how many elements are still available at the beginning of the list.
i'm trying to transfer elements from a list to another, taking them out of the first one, and to do so I did something like the following:
for x in list1:
list2.append(list1.pop(list1.index(x)))
print(list2)
I've tried doing it other ways too, substituting the append function etc., but for some reason I've found that whenever I use pop() the only elements that are actually used are the ones with odd indexes.
for example the output of the code above was:
input: list1 = [1, 7, 82, 4, 0] list2 = []
output: [1, 82, 0]
the weird thing is that as I was losing my mind over this I also tested this code to see if it was as absurd as it seemed:
for x in list:print(x)list.pop(list.index(x))
and the output was still only the odd indexes of the list. I'm sorry but by the very definition of a for cycle this makes absolutely 0 sense.
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.