[Python-ideas] Allow callable in slices

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


Given 2 callables checking when a condition arises and returning True:

    def starting_when(element):
        ...

    def ending_when(element:
        ...
Allow:

    a_list[starting_when:]

To be equivalent to:

    from itertools import dropwhile

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

And:

    a_list[:ending_when]

To:

    from itertools import takewhile

    list(takewhile(lambda x: not ending_when(x), a_list))


More information about the Python-ideas mailing list