G
G
gylah-u2021-06-24 10:56:03
Python
gylah-u, 2021-06-24 10:56:03

How to find None in a list and replace it?

Let's say I have a list: How do I find None in it and replace it with
example = ['1', '2', None]
'None'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
soremix, 2021-06-24
@SoreMix

Something like this

new_list = ['None' if el is None else el for el in example]

D
Dmitry, 2021-06-24
@LazyTalent

>>> x = ['1', '2', None]
>>> [str(i) for i in x]
['1', '2', 'None']
>>>
>>> [str(i) if i is None else i for i in x]
['1', '2', 'None']

B
bbkmzzzz, 2021-06-24
@bbkmzzzz

will replace anything that doesn't meet the condition

if something
(blank lines for example)
example = ['1', '2', None]
for pos, val in enumerate(example):
    if not val:
        example[pos] = 'None'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question