How do I remove the first item from a list?

PythonList

Python Problem Overview


How do I remove the first item from a list?

[0, 1, 2, 3][1, 2, 3]

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

Slicing:

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

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

Solution 3 - Python

>>> x = [0, 1, 2, 3, 4]
>>> x.pop(0)
0

More on this here.

Solution 4 - Python

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

>>> l = [0, 1, 2, 3, 4]
>>> l[1:]
[1, 2, 3, 4]

Solution 5 - Python

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

Solution 6 - Python

Then just delete it:

x = [0, 1, 2, 3, 4]
del x[0]
print x
# [1, 2, 3, 4]

Solution 7 - Python

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]

Solution 8 - Python

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

Solution 9 - Python

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]

Solution 10 - Python

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!

Solution 11 - Python

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]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrectangletangleView Question on Stackoverflow
Solution 1 - PythonkevpieView Answer on Stackoverflow
Solution 2 - Pythonjustin.m.chaseView Answer on Stackoverflow
Solution 3 - Pythonuser225312View Answer on Stackoverflow
Solution 4 - PythonHaesView Answer on Stackoverflow
Solution 5 - PythonZimm3rView Answer on Stackoverflow
Solution 6 - PythonmartineauView Answer on Stackoverflow
Solution 7 - PythonvertexionView Answer on Stackoverflow
Solution 8 - PythonYoni ShperlingView Answer on Stackoverflow
Solution 9 - Pythontsveti_ikoView Answer on Stackoverflow
Solution 10 - PythonBhaskar BhuyanView Answer on Stackoverflow
Solution 11 - PythonU12-ForwardView Answer on Stackoverflow