Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - List of dictionaries all values Summation
Next article icon

Sum all Items in Python List without using sum()

Last Updated : 15 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, typically the sum() function is used to get the sum of all elements in a list. However, there may be situations where we are required to sum the elements without using the sum() function. Let's explore different methods to sum the items in a list manually.

Using for loop

Using a for loop is another simple way to sum all items in list without using sum(). We can iterate through each item in the list, adding the items to a running total.

Python
#Input List  a = [1, 2, 3, 4, 5]   # Initialize a variable to hold the total sum total = 0  # Loop through each item in the list and add it to the total for num in a:     total += num  print(total)   

Output
15 

Explanation:

  • We initialize a variable total to 0.
  • We loop through each item in the list, adding the item to total.
  • After the loop, total holds the sum of all items in the list

Let's explore some more methods and see how we can sum all items in python list without using sum().

Table of Content

  • Using a recursive function
  • Using reduce from functools

Using reduce from functools

Python's reduce() function from the functools module can also be used to sum the items in a list. The reduce function applies a binary function cumulatively to the items in the list.

Python
from functools import reduce a = [1, 2, 3, 4, 5]  # Use reduce to sum the elements in the list total = reduce(lambda x, y: x + y, a)  print(total)   

Output
15 

Explanation:

  • We import the reduce() function from the functools module.
  • The reduce function takes a lambda function that adds two numbers and applies it cumulatively to the items in the list.

Using a recursive function

For a more advanced solution, we can use recursion. A recursive function calls itself until the base condition is met, summing the elements in the list.

Python
a = [1, 2, 3, 4, 5]  # Recursive function to sum the list def recursive_sum(a):     if len(a) == 0:         return 0     else:         return a[0] + recursive_sum(a[1:])  # Call the function and print the result total = recursive_sum(a) print(total)   

Output
15 

Explanation:

  • recursive_sum function calls itself with the rest of the list (excluding the first element) until the list is empty.
  • base case is when the list is empty, at which point it returns 0.
  • The sum is calculated by adding the first element of the list to the result of the recursive call on the remaining list.

Next Article
Python - List of dictionaries all values Summation
author
abhibm4518
Improve
Article Tags :
  • Python
  • Python Programs
  • python-list
Practice Tags :
  • python
  • python-list

Similar Reads

  • Get Length of a List in Python Without Using Len()
    Python len() method is the most common and widely used method for getting the length of a list in Python. But we can use other methods as well for getting the length or size of the list. In this article, we will see how to find the length of a list in Python without using the len() function. Find Th
    2 min read
  • Python - Get sum of last K list items using slice
    Accessing elements in a list has many types and variations. These are an essential part of Python programming and one must have the knowledge to perform the same. This article discusses ways to fetch the last K elements and do its summation. Let’s discuss certain solution to perform this task. Metho
    5 min read
  • Python - Pairs with Sum equal to K in tuple list
    Sometimes, while working with data, we can have a problem in which we need to find the sum of pairs of tuple list. And specifically the sum that is equal to K. This kind of problem can be important in web development and competitive programming. Lets discuss certain ways in which this task can be pe
    6 min read
  • Sum Integers Stored in JSON using Python
    Summing integers stored in JSON using Python involves reading JSON data, parsing it, and then calculating the sum of integer values. Python's json module is instrumental in loading JSON data, and the process often includes iterating through the data structure to identify and accumulate integer value
    4 min read
  • Python - List of dictionaries all values Summation
    Given a list of dictionaries, extract all the values summation. Input : test_list = [{"Apple" : 2, "Mango" : 2, "Grapes" : 2}, {"Apple" : 2, "Mango" : 2, "Grapes" : 2}] Output : 12 Explanation : 2 + 2 +...(6-times) = 12, sum of all values. Input : test_list = [{"Apple" : 3, "Mango" : 2, "Grapes" : 2
    5 min read
  • Python | Sum list of dictionaries with same key
    You have given a list of dictionaries, the task is to return a single dictionary with sum values with the same key. Let's discuss different methods to do the task. Method #1: Using reduce() + operator Step-by-step approach: Import necessary modules - collections, functools, and operator.Initialize a
    7 min read
  • How to Remove All Items From a List in Python
    Removing all items from the list can be done by using various different methods in Python. Using clear()clear() method is the simplest and most efficient way to remove all items from a list. [GFGTABS] Python a = [1, 2, 3, 4, 5] # Remove all items using clear() a.clear() # Print the empty list print(
    2 min read
  • Python | Accumulative index summation in tuple list
    Sometimes, while working with data, we can have a problem in which we need to find accumulative summation of each index in tuples. This problem can have applications in web development and competitive programming domain. Let's discuss certain way in which this problem can be solved. Method 1: Using
    8 min read
  • Python | Equal Keys List Summation
    Sometimes, while working with dictionaries, we can have a problem in which we have many dictionaries and we are required to sum like keys. This problem seems common, but complex is if the values of keys are list and we need to add elements to list of like keys. Let’s discuss way in which this proble
    4 min read
  • Python | i^k Summation in list
    Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding sum of i^k of list in just one line. Let’s discuss cer
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences