Python sum() function: Calculate sum of List, Tuple
The sum is the most frequent operation in programming, and Python is no different. Python provides the sum() function that can help you calculate the sum of numbers.
Python sum
The sum() is a built-in Python function that takes an iterable as an argument, adds the elements of an iterable, and returns the sum. The iterators can be anything like List, Tuple, Set, or Dictionary. To calculate the sum of a list, tuple, dictionary, or set in Python, use the sum() method.
Syntax
sum(iterable, start)
Arguments
The sum() function adds start and elements of the given iterators from left to right.
The iterable may be Python list, tuple, set, or dictionary.
How to find a sum of the list in Python
To find a sum of the list in Python, use the sum() method. The sum() is a built-in method that is used to get the summation of
You need to define the list and pass the list as a parameter to the sum() function, and in return, you will get the sum of list items.
Let’s take a list, apply the sum function, and see the result.
# app.py listA = [ 11, 18, 19, 21, 46 ] print(sum(listA))
See the following output.
➜ pyt python3 app.py 115 ➜ pyt
If you need to add floating-point numbers with exact precision, you should use the math.fsum(iterable) instead if you want to concatenate the items of a given iterable (items must be a string), then you can use the Python String join() method.
How to find a sum of a tuple in Python
To find a sum of the tuple in Python, use the sum() method. For example, define a tuple with number values and pass the tuple as a parameter to the sum() function, and in return, you will get the sum of tuple items.
Let’s define a tuple, pass the tuple in the sum() function, and see the output.
# app.py tupA = ( 11, 18, 19, 21, 46 ) print(sum(tupA))
See the following output.
➜ pyt python3 app.py 115 ➜ pyt
How to find Sum of Set elements in Python
To calculate the sum of set in Python, use the sum() method. First, define a set and pass the set as a parameter to the sum() function, and in return, you will get the sum of set items.
See the following code.
# app.py setA = { 11, 18, 19, 21, 46 } print(sum(setA))
See the output.
➜ pyt python3 app.py 115 ➜ pyt
Find the sum of Dictionary keys in Python.
To find a sum of dictionary keys in Python, use the sum() method. In the case of the Python dictionary, the key to the dictionary will get added. Thus, the output will be the sum of all the dictionary keys.
# app.py dictA = { 11: 'Eleven', 18: 'Dustin', 19: 'Mike', 21: 'Lucas', 46: 'Noah' } print(sum(dictA))
See the output.
➜ pyt python3 app.py 115 ➜ pyt
See, the dictionary values are String, but it does not matter here because we will add the keys and not the values.
Python sum(): error and exceptions
The error is raised when other data types are other than numbers in the list.
See the following code.
# app.py dictA = { '11': 'Eleven', '18': 'Dustin', '19': 'Mike', '21': 'Lucas', '46': 'Noah' } print(sum(dictA))
I have taken the keys as a string and not an integer in the above code. So, it will raise the following error.
➜ pyt python3 app.py Traceback (most recent call last): File "app.py", line 9, in <module> print(sum(dictA)) TypeError: unsupported operand type(s) for +: 'int' and 'str' ➜ pyt
You can convert the integer from a string to get the correct result.
Pass the second parameter
The sum() function takes the second parameter, “start,” as an optional.
It returns the following.
sum of all elements + “start”
See the following example.
# app.py listA = [ 11, 18, 19, 21, 46 ] print(sum(listA, 19))
See the output.
➜ pyt python3 app.py 134 ➜ pyt
That means 115 + 19 = 134.
Sum of float numbers in Python
To calculate the sum of float numbers in Python, use math.fsum() method. The math.fsum() is a built-in method that calculates the sum of the floating-point numbers.
If you need to add floating-point numbers with exact precision, you should use math.fsum() function instead.
import math listA = [1.1, 1.9, 2.1, 4.6] print(math.fsum(listA))
Output
9.7
If you need to concatenate items of the given iterable (items must be strings), you can use the Python join() method.
That’s it for the sum() function in Python.