The Python Oracle

How do I remove the first item from a list?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Civilization

--

Chapters
00:00 Question
00:17 Accepted answer (Score 1588)
01:09 Answer 2 (Score 252)
01:24 Answer 3 (Score 61)
01:37 Answer 4 (Score 36)
02:01 Thank you

--

Full question
https://stackoverflow.com/questions/4426...

Accepted answer links:
[here]: https://docs.python.org/3/tutorial/datas...
[list.pop(index)]: https://www.programiz.com/python-program...
[del list[index]]: https://docs.python.org/3/tutorial/datas...
[collections.deque]: http://docs.python.org/library/collectio...

Answer 3 links:
[here]: http://docs.python.org/tutorial/datastru...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #list

#avk47



ANSWER 1

Score 270


Slicing:

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

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




ANSWER 2

Score 66


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

More on this here.




ANSWER 3

Score 40


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

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



ANSWER 4

Score 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