When simultaneously looping over multiple python lists, for which I use the zip-function, I also want to retrieve the looping index. To this end, a separate list for the looping index may be included in the zip-function, e.g.:
for index, item1, item2 in zip(range(len(list1)), list1, list2):
<do something>
Is there a better way to do this (like in the enumerate-function)?
You can unpack everything in the for:
>>> for i, (x1, x2) in enumerate(zip([1,2,3], [3,4,5])):
... print i, x1, x2
...
0 1 3
1 2 4
2 3 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With