Replace None value in list?
Question:
I have:
d = [1,'q','3', None, 'temp']
I want to replace None value to ‘None’ or any string
expected effect:
d = [1,'q','3', 'None', 'temp']
a try replace in string and for loop but I get error:
TypeError: expected a character buffer object
Answers:
Use a simple list comprehension:
['None' if v is None else v for v in d]
Demo:
>>> d = [1,'q','3', None, 'temp']
>>> ['None' if v is None else v for v in d]
[1, 'q', '3', 'None', 'temp']
Note the is None
test to match the None
singleton.
Using a lengthy and inefficient however beginner friendly for loop it would look like:
d = [1,'q','3', None, 'temp']
e = []
for i in d:
if i is None: #if i == None is also valid but slower and not recommended by PEP8
e.append("None")
else:
e.append(i)
d = e
print d
#[1, 'q', '3', 'None', 'temp']
Only for beginners, @Martins answer is more suitable in means of power and efficiency
List comprehension is the right way to go, but in case, for reasons best known to you, you would rather replace it in-place rather than creating a new list (arguing the fact that python list is mutable), an alternate approach is as follows
d = [1,'q','3', None, 'temp', None]
try:
while True:
d[d.index(None)] = 'None'
except ValueError:
pass
>>> d
[1, 'q', '3', 'None', 'temp', 'None']
You can simply use map
and convert all items to strings using the str
function:
map(str, d)
#['1', 'q', '3', 'None', 'temp']
If you only want to convert the None
values, you can do:
[str(di) if di is None else di for di in d]
Starting Python 3.6 you can do it in shorter form:
d = [f'{e}' for e in d]
hope this helps to someone since, I was having this issue just a while ago.
I did not notice any answer using lambda..
Someone who wants to use lambda….(Look into comments for explanation.)
#INPUT DATA
d =[1,'q','3', None, 'temp']
#LAMBDA TO BE APPLIED
convertItem = lambda i : i or 'None'
#APPLY LAMBDA
res = [convertItem(i) for i in d]
#PRINT OUT TO TEST
print(res)
My solution was to match for None
and replace it.
def fixlist(l: list):
for i, v in enumerate(l):
if v is None:
l[i] = 0
I have:
d = [1,'q','3', None, 'temp']
I want to replace None value to ‘None’ or any string
expected effect:
d = [1,'q','3', 'None', 'temp']
a try replace in string and for loop but I get error:
TypeError: expected a character buffer object
Use a simple list comprehension:
['None' if v is None else v for v in d]
Demo:
>>> d = [1,'q','3', None, 'temp']
>>> ['None' if v is None else v for v in d]
[1, 'q', '3', 'None', 'temp']
Note the is None
test to match the None
singleton.
Using a lengthy and inefficient however beginner friendly for loop it would look like:
d = [1,'q','3', None, 'temp']
e = []
for i in d:
if i is None: #if i == None is also valid but slower and not recommended by PEP8
e.append("None")
else:
e.append(i)
d = e
print d
#[1, 'q', '3', 'None', 'temp']
Only for beginners, @Martins answer is more suitable in means of power and efficiency
List comprehension is the right way to go, but in case, for reasons best known to you, you would rather replace it in-place rather than creating a new list (arguing the fact that python list is mutable), an alternate approach is as follows
d = [1,'q','3', None, 'temp', None]
try:
while True:
d[d.index(None)] = 'None'
except ValueError:
pass
>>> d
[1, 'q', '3', 'None', 'temp', 'None']
You can simply use map
and convert all items to strings using the str
function:
map(str, d)
#['1', 'q', '3', 'None', 'temp']
If you only want to convert the None
values, you can do:
[str(di) if di is None else di for di in d]
Starting Python 3.6 you can do it in shorter form:
d = [f'{e}' for e in d]
hope this helps to someone since, I was having this issue just a while ago.
I did not notice any answer using lambda..
Someone who wants to use lambda….(Look into comments for explanation.)
#INPUT DATA
d =[1,'q','3', None, 'temp']
#LAMBDA TO BE APPLIED
convertItem = lambda i : i or 'None'
#APPLY LAMBDA
res = [convertItem(i) for i in d]
#PRINT OUT TO TEST
print(res)
My solution was to match for None
and replace it.
def fixlist(l: list):
for i, v in enumerate(l):
if v is None:
l[i] = 0