Reputation: 53031
How do I remove the first item from a list?
[0, 1, 2, 3] → [1, 2, 3]
Upvotes: 1085
Views: 1348807
Reputation: 2878
Did some timings on the methods suggested here -
from timeit import timeit
def reassign_slice(lst):
lst = lst[1:]
def delete_first_index(lst):
del lst[0]
def pop_first(lst):
lst.pop(0)
def assign_slice_as_empty_list(lst):
lst[:1] = []
def delete_slice(lst):
del lst[:1]
for suggested_method in reassign_slice, delete_first_index, pop_first, assign_slice_as_empty_list, delete_slice:
lst = list(range(10**6))
res = timeit(lambda: suggested_method(lst), number=1000)
print(f'{suggested_method.__name__} - {res}')
''' In Python 3.13.0 on Linux(OpenSUSE), I got -
reassign_slice - 3.2175468700006604
delete_first_index - 0.08350443698873278
pop_first - 0.08226470999943558
assign_slice_as_empty_list - 0.08683017300791107
delete_slice - 0.09479296299105044
'''
Upvotes: 1
Reputation: 23331
One another way to remove the first element not mentioned here is to assign an empty list to a slice containing the first element:
lst = [0, 1, 2, 3]
lst[:1] = []
print(lst) # [1, 2, 3]
Unlike list.pop()
, slicing doesn't throw an IndexError even if the list is empty, which is useful in some cases (and not useful in other cases).
lst = []
lst.pop(0) # <--- IndexError
del lst[:1] # <--- no error
lst[:] = lst[1:] # <--- no error
lst[:1] = [] # <--- no error
Upvotes: 1
Reputation: 41
This works for me, instead of using pop
like below, which of course will be 0, because the result/return value of pop
>>> x = [0, 1, 2, 3].pop(0)
>>> x
0
Using this below method will skip the first value:
>>> x = [0, 1, 2, 3][1:]
>>> x
[1, 2, 3]
Upvotes: 2
Reputation: 71610
You could use unpacking assignment as mentioned in PEP 3132.
You should try unpacking like the following:
>>> l = [0, 1, 2, 3, 4]
>>> _, *l = l
>>> l
[1, 2, 3, 4]
As mentioned in PEP 3132:
This PEP proposes a change to iterable unpacking syntax, allowing to specify a "catch-all" name which will be assigned a list of all items not assigned to a "regular" name.
An example says more than a thousand words:
>>> a, *b, c = range(5) >>> a 0 >>> c 4 >>> b [1, 2, 3]
Upvotes: 10
Reputation: 581
There is a data structure called deque
or double ended queue which is faster and efficient than a list. You can use your list and convert it to deque and do the required transformations in it. You can also convert the deque back to list.
import collections
mylist = [0, 1, 2, 3, 4]
#make a deque from your list
de = collections.deque(mylist)
#you can remove from a deque from either left side or right side
de.popleft()
print(de)
#you can covert the deque back to list
mylist = list(de)
print(mylist)
Deque also provides very useful functions like inserting elements to either side of the list or to any specific index. You can also rotate or reverse a deque. Give it a try!
Upvotes: 9
Reputation: 26108
You can find a short collection of useful list functions here.
>>> l = ['a', 'b', 'c', 'd']
>>> l.pop(0)
'a'
>>> l
['b', 'c', 'd']
>>>
>>> l = ['a', 'b', 'c', 'd']
>>> del l[0]
>>> l
['b', 'c', 'd']
>>>
These both modify your original list.
Others have suggested using slicing:
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'])
Upvotes: 1684
Reputation: 8032
If you are working with numpy you need to use the delete method:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
a = np.delete(a, 0)
print(a) # [2 3 4 5]
Upvotes: 7
Reputation: 133
You can use list.reverse()
to reverse the list, then list.pop()
to remove the last element, for example:
l = [0, 1, 2, 3, 4]
l.reverse()
print l
[4, 3, 2, 1, 0]
l.pop()
0
l.pop()
1
l.pop()
2
l.pop()
3
l.pop()
4
Upvotes: 6
Reputation: 541
You can also use list.remove(a[0])
to pop
out the first element in the list.
>>>> a=[1,2,3,4,5]
>>>> a.remove(a[0])
>>>> print a
>>>> [2,3,4,5]
Upvotes: 7
Reputation: 123521
Then just delete it:
x = [0, 1, 2, 3, 4]
del x[0]
print x
# [1, 2, 3, 4]
Upvotes: 21
Reputation: 3425
you would just do this
l = [0, 1, 2, 3, 4]
l.pop(0)
or l = l[1:]
Pros and Cons
Using pop you can retrieve the value
say x = l.pop(0)
x
would be 0
Upvotes: 37
Reputation: 13126
With list slicing, see the Python tutorial about lists for more details:
>>> l = [0, 1, 2, 3, 4]
>>> l[1:]
[1, 2, 3, 4]
Upvotes: 41
Reputation: 13685
Slicing:
x = [0,1,2,3,4]
x = x[1:]
Which would actually return a subset of the original but not modify it.
Upvotes: 273