How can I do something like the following in Python?
row = [unicode(x.strip()) if x for x in row]
Basically, a list comprehension where you carry out a function if the variable exists.
Thanks!
How can I do something like the following in Python?
row = [unicode(x.strip()) if x for x in row]
Basically, a list comprehension where you carry out a function if the variable exists.
Thanks!
The "if" goes at the end"
row = [unicode(x.strip()) for x in row if x]
So close.
row = [unicode(x.strip()) for x in row if x]
Not sure about the goals, but this should work
row = [unicode(x.strip()) for x in row if x ]
Maybe you're were thinking of the ternary operator syntax as used if you want if/else, e.g.:
row = [unicode(x.strip()) if x is not None else '' for x in row ]
or whatever you'd want to do.