Skip to content
Advertisement

Pythonic way to split a list into first and rest in Python 2.x?

I think in Python 3 I’ll be able to do:

JavaScript

which is exactly what I want, but I’m using 2.6. For now I’m doing:

JavaScript

This is fine, but I was just wondering if there’s something more elegant.

Advertisement

Answer

JavaScript

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):

JavaScript
Advertisement