Sometimes, we want to apply function to each element of a list with Python.
In this article, we’ll look at how to apply function to each element of a list with Python.
How to apply function to each element of a list with Python?
To apply function to each element of a list with Python, we use the map function.
For instance, we write
from string import upper
mylis = ['this is test', 'another test']
mapped = map(upper, mylis)
to create the mylis with some strings inside.
Then we call map to call the upper function on each entry of mylis.
An iterator is then returned with the mylis string in upper case from map.
Conclusion
To apply function to each element of a list with Python, we use the map function.