On Mon, 20 Jun 2022 at 05:32, Mike Miller <python-id...@mgmiller.net> wrote:
>
> My first thought was next(), which I use occasionally:
>
>      >>> items = (i for i in range(9))
>      >>> items
>      <generator object <genexpr> at 0x7f33251766d0>
>
>      >>> first, second = next(items), next(items)  # 👀
>
>      >>> first, second
>      (0, 1)
>
>      >>> tuple(items)
>      (2, 3, 4, 5, 6, 7, 8)
>
>
> No imports needed.  Is this deficient for the use case in some way?
>

It's fine for exactly two elements, where you'll never need to adjust
the code to want three, and where you know already that this is an
iterator (not some other iterable). If you had five elements to
unpack, it would be quite clunky, and even more so if you wanted to
change the precise number of elements unpacked, as you'd have to match
the number of next calls.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KAHHSM27WV5X3B2HMENPMY6U626MLLIK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to