How to Find the Sum of a List in Python | There are several methods to find the sum of a list in python. As we know the list is a container that stores elements of similar data types. Finding the sum of elements in a list python is an easy task.
We will see these below Python program examples:–
- Sum Of Two List In Python
- Sum Of Two Elements In List Python
- Sum Of All Elements In List Python
- Python Sum List Of Strings
- Python Sum List Of Numbers
- Sum Of a List In Python Using For Loop
- Sum Of a List In Python Using Function
Find the Sum of Two List in Python
Here, we find the sum of two lists and store it in a third list.
# Python program to find the sum of two list # take list list1 = [3,6,7,8] list2 = [5,3,7,9] print("List 1:", str(list1)) print("List 2:", str(list2)) # find sum of a list result =  for i in range(0, len(list1)): result.append(list1[i] + list2[i]) # print sum of a list print("Sum:", str(result))
We take three lists list1, list2, and result and initialize list1 and list2 with some elements and initialize the result as an empty list, use a for loop to iterate over the elements, and add corresponding elements.
Sum of Two Elements in List Python
Previously, we saw how to add two list elements. Now, we will demonstrate the python code, to sum up, two elements in the same list. We can add particular elements to the same list. The code is as follows.
list = [0,8,6] list = list + list print(list)
Here, our aim is to find the sum of two elements in the list. In the above code, we add the second element and the third element hence we get the result as 14.
Sum of All Elements in List Python
Now, we find a sum of all elements in a list. We have implemented this by using a while loop.
sum = 0 elements = 0 list = [55, 33,22] while(elements < len(list)): sum = sum + list[elements] elements += 1 print("Sum of all elements:", sum)
Sum of all elements: 110
First, we initialize sum to 0 and elements to 0 then we initialize list to sum array we iterate to while loop to add each element.
Python Program to Sum List of Strings
Now, we add a list containing integers, the python behavior of the data type will not change.
def sum_list(list): return sum([int(i) for i in list if type(i) == int or i.isdigit()]) l1 = [5, 'know', 8, 'program'] l2 = ['python', 7, 'code'] print(sum_list(l1)) print(sum_list(l2))
The above program finds the digits in the list and then returns the sum.
Python Program to Sum List of Numbers
We use sum() to add a list of numbers, sum() reduces the code length and hence makes for the programmer. The sum() is a function in python that adds the given parameters.
list = [6,5,4,3,2,1] total1 = sum(list) print(total1) total2 = sum(list , 11) print(total2)
Sum usually takes two parameters: the list and a start that is sum(list, start), the start is the optional parameter when specified start the sum function adds the specified start number to the sum of the list. In the code, the sum of the list will be 21 but when the start is added we 32.
Sum of a List in Python using For Loop
We sum all the list elements using a for loop.
i = [4,6,7,9] sum = 0 for number in i: sum = sum + number print("Sum of the list:", sum)
Sum of the list: 26
The for loop iterates over each and every element and adds all the list elements. In the code, we have initialized i to list and sum to 0 then iterated in for loop to add each element.
Sum of a List in Python using Function
We define our own function to find the sum of the list. That is we use pre-defined functions to find the sum.
def sum_of_list(list): Sum = 0 for i in range(len(list)): Sum = Sum + list[i] return Sum list = [3, 5, 4, 0] sum = sum_of_list(list) print("Sum of the list:", sum)
Sum of the list: 12
We have defined sum_of_list() which takes a list as a parameter. In the function, we initialize the sum to 0 and use a for loop to iterate over the list and return Sum.
Also See:- How to Swap Elements in List of Python
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!