Kodeclik Blog


How to apply a function to all elements of a Python list

Applying a function to each element of a list is such a handy and useful capability that Python has multiple ways to accomplish this task. We will learn all these approaches in this blogpost.
First, let us determine where you will find a need to apply the same function to all elements of a list. For instance, assume you have a list of numbers representing temperatures in Fahrenheit (degrees F). Let us assume we want to convert all these temperatures in Celsius (degrees C) and arrive at a new list of modified temperatures.

Method 1: Use a for loop

The first approach that might come to your mind is to use a for loop. Here is how that might work.
The main takeaway from the code above are the two functions f2c() and convert_temperatures(). f2c() is the function that we wish to apply to all elements of a list. f2c() takes a (single) temperature value in Fahrenheit and converts it to Celsius by applying the usual formula mapping these two temperature scales.
For ease of visual understanding we format the resulting temperature to have at most two numbers after the decimal point and return it as a floating point number. In convert_temperatures we have a for loop that goes through each element of the list, converting it (by using f2c), and adding the resulting value to a newlist which is returned. Finally, temperatures is a list that contains the list of temperatures we wish to convert.
When this program is run, we obtain the expected output:

Method 2: Use the map() function

The above approach is not that elegant. The map() function intrinsically takes a function and applies it to every element of a list so our code will be much cleaner:
Notice the sizable reduction in our convert_temperatures() function. We use the map() function to apply f2c on every element of the input list “l”. This returns a “map object” which is then converted into a list using the list() function. The output is the same as before:
How simple is that!

Method 3: Use a lambda function

A lambda function in Python is merely a small function that is often defined at the point it is used and thrown away. See the below code for how it applies to our situation. The main difference between this third approach and the previous approach is that we no longer have a separate f2c() function so our code becomes even more succinct:
We use lambda functions to define a function (without naming it), apply it to each element of a list and print it. The output is again:

Method 4: Use list comprehensions

Our final approach is to use list comprehensions. Again this gives rise to elegant code. We retain the f2c() function and use list comprehensions to arrive at the converted list of temperatures:
Note that in this case, we retain the f2c() function but use list comprehension to construct the list of converted temperatures.
So there you have it - four different ways to apply a Python function to each element of a list. Which approach is your favorite? Try them all and let us know!
If you liked this blogpost, learn how to skip values in a Python list. Also learn how to properly exit a Python function.
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2023. All rights reserved.