Your code above produces x=5 for me, not an iterator object.
Usually with an iterator object, (like a generator) use of the next()
works
Here's a short example with the fibonacci sequence. Using the yield
produces a generator object.
def iterative_fib(n):
a,b = 0,1
i=1
while i<n:
a, b = b, a+b
# print(b)
i+=1
yield b
x = iterative_fib(50)
next(x) # 12586269025
I'm not 100% sure specifically in your case, but try using next
because next
expects an iterator. If this doesn't work, then maybe produce a code example that replicates your issue.
Docs for next()
: https://docs.python.org/3/library/functions.html#next
Edit:
Seeing some other answers regarding unpacking lists, here are some other ways using *
:
a = [1,2,3,4,5]
a,b,c,d,e = [1,2,3,4,5] # you already mentioned this one
a, *b = [1,2,3,4,5] # a =[1], b=[2, 3, 4, 5]
a, *b, c, d = [1,2,3,4,5] #a =[1], b=[2,3], c=[4], d=[5]
*a, b, c = [1,2,3,4,5] # a=[1,2,3], b=[4], c=[5]
#But this wont work:
a, *b, *c = [1,2,3,4,5] # SyntaxError: two starred expressions in assignment