Search code examples
pythonlist-comprehensionsliceone-liner

Return slice from list comprehension


What's the (one-line) syntax of returning a slice from list comprehension? e.g.:

def foo(iterable):
    ls = [_ for _ in iterable]
    return ls[1:]

Solution

  • Why can't you simply slice the list comprehension?

    def foo(iterable):
        return [_ for _ in iterable][1:]