On Tue, 7 Jun 2022 at 19:09, Ben Rudiak-Gould <[email protected]> wrote:
>
> This calls the predicate once per element:
>
>     def partition(pred, iterable):
>         t1, t2 = tee((pred(x), x) for x in iterable)
>         return (x for b, x in t1 if not b), (x for b, x in t2 if b)
>
> It's kind of inefficient though.

Honestly, if it weren't that there's currently a recipe in itertools,
I think the list-based version would be the best recipe to offer. The
cost of doing the job lazily AND maintaining all the other
expectations is too high; if you really need it to be lazy, it's
probably worth seeing if one of the other demands can be relaxed (eg
if it's fine to call the predicate twice).

For the OP's task, doing the partitioning eagerly wouldn't be an issue.

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

Reply via email to