Skip to content
Advertisement

How to iterate over azure.core.paging.ItemPaged?

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?

Advertisement

Answer

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)
Advertisement