[Python] How to divert data in an iterator into two others?

Discussão em 'Python' iniciado por Stack, Março 27, 2025.

  1. Stack

    Stack Membro Participativo

    I know that I can copy an iterator using

    x1, x2 = itertools.tee(x)

    Then, in order to get two generators, I could filter:

    filter(..., x1); filter(..., x2)

    However, then I would run the same computation twice, i.e. go through x in x1 and x2.

    Thus, I would do something more efficient like that:

    x1, x2 = divert(into x1 if ... else x2, x)

    Does anything like this exist in python 3?

    Continue reading...

Compartilhe esta Página