nia4life
nia4life

Reputation: 363

Iterating Through Python List Getting TypeError: 'NoneType' object is not iterable

Using nltk to generate a list of synonyms to based on an input list of keywords.

I'm getting "TypeError: 'NoneType' object is not iterable" when nltk doesn't have a synonym (i.e., when "None" is returned).

from nltk.corpus import stopwords
from PyDictionary import PyDictionary
dictionary=PyDictionary()


inputWords = ['word1','word2','word3','word4','word5','word6','word7','word8']

synonyms = list(dictionary.synonym(i) for i in inputWords 

current output for synonyms:

[['syn1','syn2','syn3'],['syn4'],None,['syn5'],None,['syn6','syn7'],None,['syn8'],None]]

desired output for synonyms:

['syn1','syn2','syn3','syn4','syn5','syn6','syn7','syn8']

I tried:

flat_list = [item for sublist in synonyms for item in sublist]

The output:

TypeError: 'NoneType' object is not iterable

What do I write to return just the synonyms in a clean list? I prefer to use list comprehension. Any help appreciated. I'm a python noob and can't find the exact answer.

Upvotes: 0

Views: 635

Answers (4)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95873

A pythonic way to do this:

result = []
for word in input_words:
    synonyms = dictionary.synonym(word)
    if synonyms:
        result.extend(synonyms)

If you insist on using a list comprehension, you could use an assignment expression:

[synonym for word in input_words if (synonyms:=dictionary.synonym(words)) for synonym in synonyms]

But I think the first option is much preferable.

Upvotes: 0

Poopaye
Poopaye

Reputation: 44

Try:

flat_list = []

for i in range(len(synonyms):
   if isinstance(synonyms, list):
      flat_list += i

Upvotes: 1

armamut
armamut

Reputation: 1116

Here is a more pythonic one liner:

flat_list = [i for sublist in synonyms if sublist is not None for i in sublist]
flat_list
>>>
['syn1', 'syn2', 'syn3', 'syn4', 'syn5', 'syn6', 'syn7', 'syn8']

Upvotes: 1

Tom Ron
Tom Ron

Reputation: 6181

You can remove the None values and use itertools -

import itertools
list(itertools.chain(*[x for x in synonyms if x]))

Upvotes: 2

Related Questions