why i have the output of [None, None, None]

Terry Reedy tjreedy at udel.edu
Thu Apr 10 14:00:58 EDT 2014


On 4/10/2014 9:54 AM, length power wrote:
>  >>> x=['','x1','x2','x3','   ']
>  >>> x
> ['', 'x1', 'x2', 'x3', '   ']
>  >>> [print("ok") for it in x if it.strip() !=""]

Don't use comprehensions for side effects. To get what you want, just 
write normal code.

for it in x:
   if it.strip() != '':
     print('ok')

> ok
> ok
> ok
> [None, None, None]
>
> i understand there are three 'ok' in the output,but why i have the
> output of [None, None, None]

Reread the ref manual section on comprehensions.

-- 
Terry Jan Reedy




More information about the Python-list mailing list