Skip to content Skip to sidebar Skip to footer

Explain Python List Comprehension Technique

Can someone please explain this bit of code please. >>> guest=['john','sue','chris'] >>> [(a,b,c) for a in guest for b in guest for c in guest] With these result

Solution 1:

It's a nested list comprehension, and you can expand the loops in the same order they appear in the comprehension to understand what's happening:

result = []
for a in guest:
    for b in guest:
        for c in guest:
            # yield (a,b,c)
            result.append((a,b,c))

Post a Comment for "Explain Python List Comprehension Technique"