Python 3 only answer (that doesn't use slicing or throw away the rest of the list
, but might be good enough anyway) is use unpacking generalizations to get first
and last
separate from the middle:
first, *_, last = some_list
The choice of _
as the catchall for the "rest" of the arguments is arbitrary; they'll be stored in the name _
which is often used as a stand-in for "stuff I don't care about".
Unlike many other solutions, this one will ensure there are at least two elements in the sequence; if there is only one (so first
and last
would be identical), it will raise an exception (ValueError
).