Basically the same, except that it's a oneliner. Tuple assigment rocks.
This is a bit longer and less obvious, but generalized for all iterables (instead of being restricted to sliceables):
i = iter(l)
first = next(i) # i.next() in older versions
rest = list(i)
20
votes
You can do
first = l.pop(0)
and then l will be the rest. It modifies your original list, though, so maybe it’s not what you want.
0
votes
I would suggest:
first, remainder = l.split(None, maxsplit=1)
0
votes
Yet another one, working with python 2.7. Just use an intermediate function. Logical as the new behavior mimics what happened for functions parameters passing.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more
first, rest = l[0], l[1:]. - Katriel