0 votes
1 view
in Python by (32.3k points)

How can I do the following in Python?

row = [unicode(x.strip()) for x in row if x is not None else '']

Essentially:

  1. replace all the Nones with empty strings, and then

  2. carry out a function.

2 Answers

0 votes
by (76.5k points)

For using if/else in a list comprehension you can use the below-mentioned code:-

def change(f): 

if f is None: 

return unicode(f.strip()) 

else: 

return '' row = [change(x) for x in row]

0 votes
by (21.5k points)

You can try using the code given below:

def change(f):

    if f is None:

        return unicode(f.strip())

    else:

        return ''

row = [change(x) for x in row]

Although then you have:

row = map(change, row)

Or you can use a lambda inline.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !

Categories

...