Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

313
Visualizações
How to create a copy of python iterator?

In python i tried to create a copy of my iterator with using assignment however it create a copy of iterator which reference to the original iterator itself. For instance:

my_list = [5, 4, 3,2]
first_it = iter(my_list)
second_it = first_it 
print next(first_it )        #it will print 5
print next(second_it)        #it will print 4
print next(first_it )        #it will print 3

As you see in the example first_it and second_it both refer to same iterator object. Is it possible to create a copy of iterator object which is not reference to the original object?

Note This question is about how to creating a copy of iterator object by value. So don't mention for item in my_list: like solutions.
Thanks in advance

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use the itertools.tee() function to produce copies; these use a buffer to share results between different iterators:

from itertools import tee

my_list = [5, 4, 3,2]
first_it = iter(my_list)
first_it, second_it = tee(first_it)
print next(first_it)   # prints 5
print next(second_it)  # prints 5
print next(first_it)   # prints 4

Note that you should no longer use the original iterator; use only the tees.

Note that the buffer also means that these can incur a significant memory cost if you advance one of the copies far ahead of the others! From the documentation:

This itertool may require significant auxiliary storage (depending on how much temporary data needs to be stored). In general, if one iterator uses most or all of the data before another iterator starts, it is faster to use list() instead of tee().

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda