pythonlistfunctioncdr

Python function that returns tail of list


Does there exist a python function that returns the tail of a list? For the purposes of doing something succinct like:

>>> triplets = [[1,2,3],[1,2,3],[1,2,3]]
>>> [cdr(t) for t in triplets]
[[2,3],[2,3],[2,3]]

N.B. This is a different scenario that does not call for in-place operators mentioned in similar questions - the comprehension here would fail, as these methods do not return a mutated list.


Solution

  • Use list slicing :

    >>> triplets = [[1,2,3],[1,2,3],[1,2,3]]
    >>> [t[1:] for t in triplets]
    [[2,3],[2,3],[2,3]]