Python — If Else Condition in Lambda Expression

I was writing a lambda function to decode unicode into UTF8 but needed a conditional as some items would be None.

The syntax for inline if else is..

'result' if 'condition' else 'else result'
map(lambda x: x.encode('UTF8') if isinstance(x, unicode) else x.__str__(), mylist)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s