Search code examples
pythongeneratorlist-comprehension

The order of nested list comprehension and nested generator expression in python


I'm new to Python and is confused by a piece of code in Python's official documentation.

unique_words = set(word  for line in page  for word in line.split())

To me, it looks equivalent to:

unique_words=set()
for word in line.split():
    for line in page:
        unique_words.add(word)

How can line be used in the first loop before it's defined in the nested loop? However, it actually works. I think it suggests the order of nested list comprehension and generator expression is from left to right, which contradicts with my previous understanding.

Can anyone clarify the correct order for me?


Solution

  • word for line in page for word in line.split()

    this part works like this:-

    for line in page:
        for word in line.split():
            print word
    

    () this makes it `generator function hence overall statement work lie this:-

    def solve():
        for line in page:
            for word in line.split():
                yield word
    

    and set() is used to avoid duplicacy or repetition of same word as the code is meant to get 'unique words'.