Categories
Python Answers

How to apply function to each element of a list with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *