[Python-ideas] Allow callables in slices

Michel Desmoulin desmoulinmichel at gmail.com
Sat Jun 9 05:17:05 EDT 2018


Such as that:

    def starting_when(element):
        ...

    a_list[starting_when:]

Is equivalent to:

    from itertools import dropwhile

    def starting_when(element):
        ...

    list(dropwhile(lambda x: not starting_when(x), a_list))

And

    def ending_when(element:
        ...

    a_list[:ending_when]

To:

    from itertools import dropwhile

    def ending_when(element:
        ...

    list(itertools.takwhile(lambda x: not condition(x), a_list))


More information about the Python-ideas mailing list