How do I enumerate() over a list of tuples in Python?
Asked Answered
S

4

59

I've got some code like this:

letters = [('a', 'A'), ('b', 'B')]
i = 0
for (lowercase, uppercase) in letters:
    print "Letter #%d is %s/%s" % (i, lowercase, uppercase)
    i += 1

I've been told that there's an enumerate() function that can take care of the "i" variable for me:

for i, l in enumerate(['a', 'b', 'c']):
    print "%d: %s" % (i, l)

However, I can't figure out how to combine the two: How do I use enumerate when the list in question is made of tuples? Do i have to do this?

letters = [('a', 'A'), ('b', 'B')]
for i, tuple in enumerate(letters):
    (lowercase, uppercase) = tuple
    print "Letter #%d is %s/%s" % (i, lowercase, uppercase)

Or is there a more elegant way?

Sputter answered 11/5, 2009 at 18:20 Comment(1)
enumerate vs itertools: what should we take into account when choosing between them?Aerometeorograph
C
142

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)
Cassirer answered 11/5, 2009 at 18:23 Comment(4)
What is this called? I want to look up more about this syntax.Risteau
@Zak: If you mean assigning to (lowercase, uppercase), it's called "unpacking".Cassirer
Neat. I didn't know that the unpacking syntax can be nested.Shaikh
Awesome! I always wrote a helper generator to achieve this. I didn't know that nesting is possible. Came across this question by lucky accident. Thanks!Inure
R
4

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
Rabiah answered 11/5, 2009 at 19:39 Comment(0)
B
1

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])
Barbour answered 10/12, 2012 at 17:5 Comment(0)
I
1

You could also write a generator:

def enumerate_nested(nested_collection, start=0):

    for index, row in enumerate(nested_collection, start):
        yield index, *row

Which then allows you to iterate over the collection of tuple (or list) and to unpack the values:

names = [["Heinz", "Steiner"], ["Fred", "Glauser"], ["Nicole", "Hauser"]]

for index, first_name, last_name in enumerate_nested(names, 1):
    print(index, first_name, last_name)

However, there is a simpler built-in solution to achieve this. Have a look at Richie Hindle's answer:

names = [["Heinz", "Steiner"], ["Fred", "Glauser"], ["Nicole", "Hauser"]]

for index, (first_name, last_name) in enumerate(names, 1):
    print(index, first_name, last_name)
Inure answered 29/9, 2022 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.