• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Sum Of Elements In A List In Python

Sum Of Elements In A List In Python

Author: Aditya Raj
Last Updated: January 9, 2022

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.   

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us