You don't need any conditional expression* here, as str.split()
always returns a list, even if only containing one word:
lst[:] = [word for words in lst for word in words.split()]
Demo:
>>> lst = ['word','word','multiple words','word']
>>> [word for words in lst for word in words.split()]
['word', 'word', 'multiple', 'words', 'word']
The conditional expression can be used wherever you could use a simple expression in the syntax; that means anywhere it says expression
or old_expression
in the list display grammar:
list_display ::= "[" [expression_list | list_comprehension] "]"
list_comprehension ::= expression list_for
list_for ::= "for" target_list "in" old_expression_list [list_iter]
old_expression_list ::= old_expression [("," old_expression)+ [","]]
old_expression ::= or_test | old_lambda_expr
list_iter ::= list_for | list_if
list_if ::= "if" old_expression [list_iter]
So the first part of a list comprehension, but also the part that produces the outermost iterator (evaluated once), the if
expressions, or any of the nested iterators (evaluated each iteration of the next outer for
loop).
*It's called the conditional expression; it is a ternary operator, but so is the SQL BETWEEN
operator.