views:

60

answers:

1

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")]
+6  A: 
>>> [(i, j) for i, _, k in myList for j in k]
[('Foo', 1), ('Foo', 2), ('Foo', 3), ('Bar', 'i'), ('Bar', 'j')]
SilentGhost