How do I enumerate() over a list of tuples in Python?
Solution 1:
This is a neat way to do it:
letters = [('a', 'A'), ('b', 'B')]
for i, (lowercase, uppercase) in enumerate(letters):
print "Letter #%d is %s/%s" % (i, lowercase, uppercase)
Solution 2:
This is how I'd do it:
import itertools
letters = [('a', 'A'), ('b', 'B')]
for i, lower, upper in zip(itertools.count(),*zip(*letters)):
print "Letter #%d is %s/%s" % (i, lower, upper)
EDIT: unpacking becomes redundant. This is a more compact way, which might work or not depending on your use case:
import itertools
letters = [('a', 'A'), ('b', 'B')]
for i in zip(itertools.count(),*zip(*letters)):
print "Letter #%d is %s/%s" % i
Solution 3:
You can do this way too:
letters = [('a', 'A'), ('b', 'B')]
for i, letter in enumerate(letters):
print "Letter #%d is %s/%s" % (i, letter[0], letter[1])