Python lists are one of the most used data structures. We often need to perform different operations on lists. In this article, we will discuss different ways to find the sum of elements in a list in python.
Find Sum Of Elements In A List Using For Loop
The first way to find the sum of elements in a list is to iterate through the list and add each element using a for loop. For this, we will first calculate the length of the list using the len() method. After that, we will declare a variable sumOfElements
to 0. After that, we will use the range() function to create a sequence of numbers from 0 to (length of the list-1)
. Using the numbers in this sequence, we can access the elements of the given list and add them to sumOfElements
as follows.
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("The given list is:")
print(myList)
list_length=len(myList)
sumOfElements=0
for i in range(list_length):
sumOfElements=sumOfElements+myList[i]
print("Sum of all the elements in the list is:", sumOfElements)
Output:
The given list is:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Sum of all the elements in the list is: 45
Alternatively, we can directly iterate through the list using a for loop. Here, we will access each element in the list directly and add them to sumOfElements
as follows.
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("The given list is:")
print(myList)
sumOfElements = 0
for element in myList:
sumOfElements = sumOfElements + element
print("Sum of all the elements in the list is:", sumOfElements)
Output:
The given list is:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Sum of all the elements in the list is: 45
Find Sum Of Elements In A List Using While Loop
We can also use a while loop to find the sum of the elements in the list. For that, we will first calculate the length of the list using the len() method. After that, we will initialize variables named count and sumOfElements
. We will initialize both the elements to 0.
In the while loop, we will access each element in the list using the count variable and add them to sumOfElements
. After that, we will increment the value of the count by 1. We will continue this process until the count becomes equal to the length of the list.
You can write a program to find the sum of elements in a list in python as follows.
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("The given list is:")
print(myList)
list_length = len(myList)
sumOfElements = 0
count = 0
while count < list_length:
sumOfElements = sumOfElements + myList[count]
count = count + 1
print("Sum of all the elements in the list is:", sumOfElements)
Output:
The given list is:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Sum of all the elements in the list is: 45
Sum Of Elements In A List Using The sum() Function
Python also provides us with an inbuilt sum() function to calculate the sum of the elements in any collection object. The sum() function accepts an iterable object such as list, tuple, or set and returns the sum of the elements in the object.
You can find the sum of the elements of a list using the sum() function as follows.
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("The given list is:")
print(myList)
sumOfElements = sum(myList)
print("Sum of all the elements in the list is:", sumOfElements)
Output:
The given list is:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Sum of all the elements in the list is: 45
Conclusion
In this article, we have discussed different ways to find the sum of elements in a list in python. To read more about lists in python, you can read this article on how to compare two lists in python. You might also like this article on list comprehension.
The post Sum Of Elements In A List In Python appeared first on PythonForBeginners.com.
from Planet Python
via read more
No comments:
Post a Comment