The Python Oracle

How do I remove the first item from a list?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4

--

Chapters
00:00 How Do I Remove The First Item From A List?
00:12 Accepted Answer Score 1659
00:53 Answer 2 Score 268
01:03 Answer 3 Score 65
01:13 Answer 4 Score 39
01:25 Answer 5 Score 36
01:43 Thank you

--

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

--

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