Python how to replace a None value in list with string None

Question:

I have list:

test_list = ['one','two',None]

Is there any simple way to replace None with ‘None’ ,without using index, because index for None maybe different each time.

I tried :

conv = lambda i : i or 'None'
res = [conv(i) for i in test_list] 

It works ,is there another way to do so ?

Asked By: William

||

Answers:

In this way all the data types would be converted to string

test_list = ['one','two',None]    
res = [str(i) for i in test_list]

In this the data type will also be preserved

res = ['None' if i is None else i for i in test_list]
Answered By: Harsh Srivastava
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.