How can I do the following in Python?
row = [unicode(x.strip()) for x in row if x is not None else \'\']
Essentially:
Here is another illustrative example:
>>> print(", ".join(["ha" if i else "Ha" for i in range(3)]) + "!")
Ha, ha, ha!
It exploits the fact that if i
evaluates to False
for 0
and to True
for all other values generated by the function range()
. Therefore the list comprehension evaluates as follows:
>>> ["ha" if i else "Ha" for i in range(3)]
['Ha', 'ha', 'ha']