Python sum | python sum list | sum() function in Python

In this post, we will see about Python sum () function.
Python sum() function is used to sum or add elements of the iterator from start to the end of iterable. It is generally used with numbers only.

Introduction

Python 3 comes with many built-in functions that you can readily use in any program that you’re working on. Some functions enable you to convert data types, and others are specific to a certain type, like strings.

The sum() function returns a number, the sum of all items in an iterable.

Syntax of sum

sum(iterable, start) 

Parameters

iterable: iterable can be anything list, tuples or dictionaries,
but most importantly it should be number.
start : [optional] this start is added to the sum of numbers in the iterable.
If start is not given in the syntax, it is assumed to be 0.

Note: Here the iterable maybe Python list, tuple, set, or dictionary.

What sum function returns in Python

The sum function returns

  • Sum of the iterable from left to right
  • If start is provided, it returns start + sum of iterable from left to right

The time complexity of sum()

The time complexity of Python sum() depends on your data structure. For a flat list, dict you cannot do better than O(n) because you have to look at each item in the list to add them up.

Python program to calculate the sum of elements in a list

Sum of Python list

To add all the elements of a list, a solution is to use the built-in function sum(), illustration:

list = [2, 3, 5, 8]
sum(list)

Output

18
Python Program to Sum the list with start 10
list = [2, 3, 5, 8]
print(sum(list, 10))

Output

18
Program to Sum the list of float
fList=[1.2,2.3,3.2,4.9]
print(sum(fList))

Output

11.600000000000001
Python Program to Sum the list of float with start 10.1
fList=[1.2,2.3,3.2,4.9]
print(sum(fList,10.1))

Output

21.699999999999996

Program to calculate the sum of elements in a tuple

Example 1: Adding all items in a tuple, and returning the result
tuple = (1, 2, 3, 4, 5)
print(sum(tuple))

Output

15
Example 1: Starting with the 10, and adding all items in a tuple to this number:
tuple = (1, 2, 3, 4, 5)
print(sum(tuple, 10))

Output

25

Python Program to calculate the sum of complex numbers

In sum() function works with complex numbers too.

s = sum([1 + 2j, 3 + 4j])
print(s)

s = sum([1 + 2j, 3 + 4j], 2 + 2j)
print(s)

s = sum([1 + 2j, 2, 1.5 - 2j])
print(s)

Output

(4+6j) (6+8j) (4.5+0j) 

Program to calculate the sum of sets

The sum() function works with the set too.

sett = {1, 2, 3, 4, 5}
print(sum(sett))

Output

15

Python Program to use the sum function in a dictionary

In the case of the Python dictionary, the key to the dictionary will get added. The output will be the sum of all the keys of the dictionary.

dict = {1: "one", 2: "two", 3: "three"}
print(sum(dict))

Output

6

See the values of the dictionary are String, but it does not matter here because we will add the keys and not the values.

Working of sum() function in list of “strings”:

>>> list = ['a','b','c','d']
>>> sum(list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Note: in the case of a list of strings the function sum() does not work.

Also Read:

Python Remove Duplicates From List
Python Print Without Newline
Learn Python the Hard Way Review PDF
How to Get a Data Science Internship With No Experience
Python Map

Alternative of sum

For some use cases, there are good alternatives to sum(). The preferred, fast way to concatenate a sequence of strings is by calling “ ”.join(sequence). To add floating point values with extended precision, see math.fsum(). To concatenate a series of iterables, consider using itertools.chain().

Conclusion

So in this tutorial, we have tried to cover all the details and examples of Python sum function. But still, if you have any questions and suggestions comment down below.

Happy Coding!

Leave a Reply