1031

How do I remove the first item from a list?

[0, 1, 2, 3]   →   [1, 2, 3]
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
rectangletangle
  • 50,393
  • 94
  • 205
  • 275

12 Answers12

1620

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
Neuron
  • 5,141
  • 5
  • 38
  • 59
kevpie
  • 25,206
  • 2
  • 24
  • 28
259

Slicing:

x = [0,1,2,3,4]
x = x[1:]

Which would actually return a subset of the original but not modify it.

justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
  • 36
    if `x` is empty, `x=x[1:]` would leave it empty without complaining. `x.pop(0)` would throw for an empty list `x`. Sometimes throwing is what one wants: If the assumption that there is at least an element in the list is wrong, one might want to get notified. – ead Jul 09 '16 at 18:50
  • 4
    I like this reply much more, as mutations is almost always evil. – Onkeltem Jan 24 '21 at 11:28
  • If performance is a concern, pop seems to perform faster than slice: ["slicing the list carries a performance penalty of ~50% compared to just doing a pop of the first element. This penalty seems to plateau after a certain list size."](https://esaezgil.com/post/python_list_pop_slice/). – Martin del Necesario Apr 11 '23 at 09:59
63
>>> x = [0, 1, 2, 3, 4]
>>> x.pop(0)
0

More on this here.

user225312
  • 126,773
  • 69
  • 172
  • 181
36

With list slicing, see the Python tutorial about lists for more details:

>>> l = [0, 1, 2, 3, 4]
>>> l[1:]
[1, 2, 3, 4]
Haes
  • 12,891
  • 11
  • 46
  • 50
36

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

user225312
  • 126,773
  • 69
  • 172
  • 181
Zimm3r
  • 3,369
  • 5
  • 35
  • 53
21

Then just delete it:

x = [0, 1, 2, 3, 4]
del x[0]
print x
# [1, 2, 3, 4]
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    For me this is the best answer related to the question (if you want to keep the original list object). Slicing `x[1:]` does not keep the original object but might be quicker. `pop(0)` is not required if the removed object should end up in the garbage collector. – B.R. Mar 11 '23 at 22:36
11

Unpacking assignment:

You could use unpacking assignment as mentioned in PEP 3132.

Solution:

You should try unpacking like the following:

>>> l = [0, 1, 2, 3, 4]
>>> _, *l = l
>>> l
[1, 2, 3, 4]

Explanation:

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]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    This solution (which has been around since Python 3.0) should definitively be preferred if you need both, head *and* tail. Do you perhaps have some additional information at hand about the performance compared to `pop(0)`? Many thanks in advance. – Wolf May 08 '23 at 10:34
9

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]
vertexion
  • 536
  • 2
  • 7
  • 20
  • Why is this better than `a.pop(0)` or `del x[0]`? – Ned Batchelder Aug 24 '14 at 14:19
  • 4
    OP is not asking about the best way to do it. This is just another approach to achieve the same! – vertexion Aug 24 '14 at 14:27
  • 1
    Yes, another approach that has no advantage over the other answers. There are any number of ways to do it. Why not give an answer of `a.remove(a[1-1])`? That's another way. – Ned Batchelder Aug 24 '14 at 14:29
  • You are free to add that another way! you have no objection over other answer that are suggesting exact same way to achieve the same! – vertexion Aug 24 '14 at 14:32
  • 2
    My point is that your answer is worse that the other answers, and has nothing to recommend it. There is no reason to use this answer over the others. Just because it's "another way" isn't a reason to add it here. – Ned Batchelder Aug 24 '14 at 14:44
  • 4
    @NedBatchelder Your point is moot. It's a extra method available to lists in Python, specific to this particular task, and for the sake of completeness it should be noted. The contrived BS a[1-1] example, on the other hand, it's not. Not to mention that his answer is not "worse than the other answers" in any way. – Hejazzman Nov 08 '14 at 09:09
  • 8
    I stand by my question: this seems weird and contrived, and has nothing to recommend it. In fact, the first sentence is misleading, because you cannot remove the i'th element with `list.remove(a[i])`. With duplicate values, it may find an earlier element with the same value, and remove that one instead of the i'th. – Ned Batchelder Nov 08 '14 at 13:13
  • Hey @NedBatchelder, the answer has been edited to reflect your suggestion, since it is the first element OP is asking about, it won't matter if there are duplicates or not as long as we are removing `a[0]`, Thank you! – vertexion Feb 03 '19 at 14:46
9

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!

Neuron
  • 5,141
  • 5
  • 38
  • 59
Bhaskar Bhuyan
  • 501
  • 6
  • 16
  • It is only more efficient in some cases, not across the board. It has different tradeoffs than list, and why both exist. – Gringo Suave Mar 21 '21 at 01:07
8

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
Mohd
  • 5,523
  • 7
  • 19
  • 30
Yoni Shperling
  • 137
  • 1
  • 3
  • 5
    Why do it so complicated when you can use `l.pop(0)`? You forgot to `l.reverse()` after using `l.pop()`. Your solution has bad performance – Christoph S. Dec 12 '20 at 18:50
7

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]
tsveti_iko
  • 6,834
  • 3
  • 47
  • 39
2

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]
Anurag A S
  • 725
  • 10
  • 23
MUWA
  • 33
  • 3