When running a one variable loop, adding an index variable is easy:
a = ( (2,3), (7,9) )
for i,v in enumerate( a ):
print( v )
print( i )
Yields:
(2, 3)
0
(7, 9)
1
Trying to generate a two-variable loop works well too:
for c,v in enumerate( a ):
print( c )
print( v )
Yields:
0
(2, 3)
1
(7, 9)
However, adding an index, via enumerate, for 3 loop variables, does not work:
for i,c,v in enumerate( a ):
print( c )
Yields:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)
CodePudding user response:
enumerate
returns an iterator with pairs, never triples. For this particular input data, the second member of such a pair is a pair itself. So there is nesting. You should unpack that structure using the same nesting:
for i, (c, v) in enumerate( a ):
print(f"i={i} c={c} v={v}")