How To Sum A List Of Float Numbers In Python

Sum a list of float numbers in Python

The topic of sum a list of float numbers in Python is relatively easy because in Python, there is support for built-in functions to do this. For example, the math library’s fsum function and the sum function, or you can use a for loop to sum a list of float numbers. Read the following article to understand more.

Sum a list of float numbers in Python

Use the math.fsum function

Syntax:

math.fsum(iterable)

Parameters:

  • iterable: Required parameter. Iterable objects such as lists or tuple.

The math.fsum function returns the sum of the elements of an iterable object (like list or tuple).

Note: If the arguments passed to the function are iterable objects other than numbers, the function will return a TypeError.

Example:

  • List of floats.
  • Use the math.fsum to sum the floats in a list.
import math

foatList=[1.2, 2.4, 1.0, 0.0, 4.2, 0.25]

# Use the math.fsum to sum the floats in a list.
result = math.fsum(foatList)
print('Sum of floats in the list:', result)

Output:

Sum of floats in the list: 9.05

If the elements in your list are not floats yet, you can use the map function to convert all the elements in the list to floats.

Syntax:

map(func,iterable)

Parameters:

  • func: given function to iterate over the elements of iterable.
  • iterable: the object you want to browse.

Example:

  • List of integers and floats.
  • Use the map function to convert the elements in the list to float.
  • Use the sum to sum the floats in a list.
import math

foatList=[1.2, 2.4, 1, 0, 4.2, 0.25]

# Use the map function to convert the elements in the list to float.
foatList = map(float, foatList)

# Use the math.fsum to sum the floats in a list.
result = math.fsum(foatList)
print('Sum of floats in the list:', result)

Output:

Sum of floats in the list: 9.05

Use the sum function

You can use the sum function to sum the floats in the list. The sum function has the same function and syntax as the fsum function in the math library.

Example:

  • List of floats.
  • Use the sum to sum the floats in a list.
foatList=[1.2, 2.4, 1.0, 0.0, 4.2, 0.25]

# Use the sum to sum the floats in a list.
result = sum(foatList)
print('Sum of floats in the list:', result)

Output:

Sum of floats in the list: 9.05

If the elements in your list are not floats yet, you can use the map function to convert all the elements in the list to floats.

Example:

  • List of integers and floats.
  • Use the map function to convert the elements in the list to float.
  • Use the sum to sum the floats in a list.
foatList=[1.2, 2.4, 1, 0, 4.2, 0.25]

# Use the map function to convert the elements in the list to float.
foatList = map(float, foatList)

# Use the sum to sum the floats in a list.
result = sum(foatList)
print('Sum of floats in the list:', result)

Output:

Sum of floats in the list: 9.05

Use the for loop

Example:

foatList=[1.2, 2.4, 1.0, 0.0, 4.2, 0.25]

result = 0
for x in foatList:
    result = result + x 
    
print('Sum of floats in the list:', result)

Output:

Sum of floats in the list: 9.05

Summary

Those are three ways to sum a list of float numbers in Python. This topic is simple, so you can use all three methods above. Thank you for reading the article. If you have any suggestions, leave us a comment below, and we will try to answer.

Maybe you are interested:

Leave a Reply

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