Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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 (20.3k 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.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...