List comprehension from multiple sources in Python?

Posted by Noah on Stack Overflow See other posts from Stack Overflow or by Noah
Published on 2010-05-05T12:50:29Z Indexed on 2010/05/05 12:58 UTC
Read the original article Hit count: 233

Filed under:
|
|

Is it possible to replace the following with a list comprehension?

res = []
for a, _, c in myList:
    for i in c:
        res.append((a, i))

For example:

# Input
myList = [("Foo", None, [1, 2, 3]), ("Bar", None, ["i", "j"])]

# Output
res = [("Foo", 1), ("Foo", 2), ("Foo", 3), ("Bar", "i"), ("Bar", "j")]

© Stack Overflow or respective owner

Related posts about python

Related posts about list-comprehension