Skip to content Skip to sidebar Skip to footer

If/else In Python List Comprehension

I would like to return random word from file, based on passed argument. But if the argument doesn't match anythning I dont want to return anything. My method looks like: def word_f

Solution 1:

Should be:

defword_from_score(self,score):
    print(random.choices([(word ifsum([LETTER_SCORES[letter] for letter in word ]) == score else"") for word in self.file]))

The (... if ... else ...) is actually the ternary operator and not part of the surrounding list comprehension.

Post a Comment for "If/else In Python List Comprehension"