eng_mazzy
eng_mazzy

Reputation: 1049

Nested list comprehension doesn't append element

I have this iteration

for item in result:
   for subitem in item.find_all('strong'):
      line = subitem.get_text()
        if line:
          temp.append(line)

It works well but I would like to implement it through list comprehension. Here is my attempt

[subitem.get_text() for subitem in item.find_all('strong') if subitem.get_text() for item in result]

but something doesn't work as it should be.

Upvotes: 0

Views: 55

Answers (1)

chepner
chepner

Reputation: 531440

The for clauses in a list comprehension should appear in the same order they would in the analogous nested loop.

[subitem.get_text() for item in result for subitem in item.find_all('strong') if subitem.get_text()]

Upvotes: 4

Related Questions