5

I have an iterator object <iterator object azure.core.paging.ItemPaged at 0x7fdb309c02b0>. When I iterate over it for a first time (see code below), it prints the results. However, when I execute this code a second time, it prints nothing.

for i, r in enumerate(result):
   print(r)

What is wrong in my code? Do I need to somehow reset the enumerator?

Fluxy
  • 2,838
  • 6
  • 34
  • 63

1 Answers1

6

This is the default behavior of the iterator in python.

If you want the iterator still work in the 2nd time, you can use itertools.tee() function to create a second version of your iterator. Like below:

from itertools import tee

#use the tee() function to create another version of iterator. here, it's result_backup
result, result_backup = tee(result)

print("**first iterate**")

for i, r in enumerate(result):
    print(r)


print("**second iterate**")

#in the 2nd time, you can use result_backup
for i, r in enumerate(result_backup):
    print(r)
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • 1
    Thanks, I used your solution but the program faced error: "Parsed api-version 2022-05-13 is not available. Please use one of the following versions: 2022-04-20-preview,0.1-preview" How I can change the api-version in python? – amin Oct 11 '22 at 21:15