python replace null in list 1

python replace null in list

>>> d = [1,'q','3', None, 'temp']
>>> ['None' if v is None else v for v in d]
[1, 'q', '3', 'None', 'temp']

Here is what the above code is Doing:
1. The first line creates a list of values.
2. The second line creates a new list.
3. The third line is a for loop that iterates over the values in the original list.
4. The fourth line is an if statement that checks if the value is None.
5. The fifth line is the code that gets executed if the if statement is True.
6. The sixth line is the code that gets executed if the if statement is False.
7. The seventh line is outside the if statement.

Similar Posts