I want to loop through a list in several loops always starting with the item of the last loop:
import itertools as it
list1=[1,2,3,4,5,6,7,8]
a=iter(list1)
while a.next()!= 8:
a,b=it.tee(a) #copy the iterator
while b.next()!=8:
b,c=it.tee(b)
while c.next()!=8:
print "yaaay"
in this code I can start my loop with the current iterator of the outer loop. How to do this in a more pythonic way not using slice?
here is an example of what I’m thinking of a more pythonic way:
list1=[1,2,3,4,5,6,7,8]
a=iter(list1)
for k1 in list1:
for k2=k1 in list1:
for k3=k2 in list1:
print "yaaay"
From my understanding, you’re looking for the way to “save” the generator state at some point and then “restore” it. Using
teeis the correct idea, PEP 0323 has more info on this.Update: