Sometimes, we want to replace values in list using Python.
In this article, we’ll look at how to replace values in list using Python.
How to replace values in list using Python?
To replace values in list using Python, we can use list comprehension.
For instance, we write
new_items = [x if x % 2 else None for x in items]
to loop through the items in list item
and return value x
in items
if it’s odd and None
otherwise and put them in the new list.
Conclusion
To replace values in list using Python, we can use list comprehension.