Add Float to Each Element in List in Python (4 Examples)
In this tutorial, you’ll learn how to add a float to each element in a list in the Python programming language.
The content is structured as follows:
Let’s go straight to the code!
Create Example Data
The first step in this tutorial is to define some example data. We will create a list object named my_list.
my_list = [5, 2, 4, 3, 8, 10] print(my_list) # [5, 2, 4, 3, 8, 10]
The previous output shows that our example list object contains six integers. What if we want to add a float to each of these elements? Let’s see how to do it!
Example 1: Increase Numeric Values in List by Float Number Using List Comprehension
In this example, we use the list comprehension method to create a new list where each element is the original element plus the float value.
float_val = 0.5 my_list = [x + float_val for x in my_list] print(my_list) # [5.5, 2.5, 4.5, 3.5, 8.5, 10.5]
As shown, the float value, defined as float_val = 0.5
, is added to every element in my_list. The printed list contains the updated float values.
Example 2: Increase Numeric Values in List by Float Number Using map() Function
The map() function can also be handy when increasing each element in a list by a float number.
float_val = 0.5 my_list = list(map(lambda x: x + float_val, my_list)) print(my_list) # [5.5, 2.5, 4.5, 3.5, 8.5, 10.5]
The above Python output shows how the map() function applies a lambda function, which adds the float value to every element. Then, the resulting map object is converted to a list using the list() function and assigning it to ‘my_list’. Let’s see the next alternative!
Example 3: Increase Numeric Values in List by Float Number Using For Loop
This first example shows how to add a float value to each element of a list using a for loop. Take a look at the code below.
float_val = 0.5 for i in range(len(my_list)): my_list[i] += float_val print(my_list) # [5.5, 2.5, 4.5, 3.5, 8.5, 10.5]
As you can see, the new list is assigned to the variable ‘my_list’ and then printed out. The output is the same as in the previous ones, great!
Example 4: Increase Numeric Values in List by Float Number Using NumPy
This example shows how to use the NumPy library to add a float value to the elements in my_list. First, we will import the library as follows.
import numpy as np
Then we will convert my_list to a NumPy array using the np.array() function to sum the float value with the entire array using the ‘+’ operator.
float_val = 0.5 my_list = np.array(my_list) + float_val print(my_list) # [ 5.5 2.5 4.5 3.5 8.5 10.5]
Finally, the updated array is printed. We can convert it back to a list using the tolist() method as follows.
my_list = my_list.tolist() print(my_list) # [5.5, 2.5, 4.5, 3.5, 8.5, 10.5]
Well done! Now we know 4 different ways to add a float number to a list of values. See you in the next tutorial!
Video, Further Resources & Summary
Do you need more explanations on how to add a float to each element in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
In addition, you may want to have a look at the related programming tutorials on Statistics Globe:
- Add Character String to List in Python
- Convert Binary List to Decimal & Vice Versa in Python
- Convert List from Character String to Integer in Python
- List in Python
- Learn Python
This post has shown how to convert each integer of a list in float using Python. In case you have further comments and/or questions, don’t hesitate to let me know in the comments.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter