Home > Software engineering >  Python how to replace a None value in list with string None
Python how to replace a None value in list with string None

Time:11-09

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 ?

CodePudding user response:

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]
  • Related