Sum All the Items in Python List Without using sum()

In this Python tutorial, we will be discussing various ways, to sum all the items in Python list without using sum() function. Summing the items in a list is a common task in programming, and Python provides a built-in function called sum() that makes it easy to do this.

However, sometimes you may need to write your own program to sum the items in a list, without using the built-in function. This can be useful if you need more control over the calculation or if you want to learn how to do it yourself.

Sum All the Items in Python List Without using sum()

We will be discussing ways to achieve this using a for and while loop, recursion, itertools.accumulate function, enumerate and list comprehension. Each method has its own advantages and disadvantages, and we will walk through the steps of each method and the trade-offs of using them.

  • Sum All the Items in Python List Using a For Loop
  • Sum All the Items in Python List Using the reduce()
  • Sum All the Items in Python List Using a While Loop
  • Sum All the Items in Python List Using Recursion
  • Sum All the Items in Python List Using the accumulate()
  • Sum All the Items in Python List Using List Comprehension
  • Sum All the Items in Python List Using Enumerate
READ:  How to fix SyntaxError invalid character in identifier in Python? [3 Examples]

Sum All the Items in Python List Using a For Loop

Initialize a variable called “result” to 0.

my_list = [1, 2, 3, 4, 5]
result = 0

Use a for loop to iterate through each item in the list. Within the for loop, add the current item to the “result” variable.

for num in my_list:
    result += num

Print the “result” variable.

print(result)
Sum All the Items in a List Using a For Loop
Example-1: Sum All the Items in Python List Without using sum()

Read: Sum of even digits of a number in Python

Sum All the Items in Python List Using the reduce() function

Import the reduce() function from the functools library and create a list my_list.

from functools import reduce
my_list = [1, 2, 3, 4, 5]

Create a variable called result and set it equal to the result of calling the reduce() function with a lambda function for addition and the list as an argument.

result = reduce(lambda x,y: x+y, my_list)

Print the result.

print(result)
Sum All the Items in Python List Without using sum()
Example-2: Sum All the Items in Python List Without using sum()

Read: How to find the length of a string in Python

Sum All the Items in Python List Using a While Loop

Initialize a variable called “result” to 0.

my_list = [1, 2, 5, 4, 5]
result = 0

Initialize a variable called “i” to 0.

i = 0

Start a while loop that continues as long as “i” is less than the length of the list. Within the while loop, add the item at the “i”th index of the list to the “result” variable. Increment “i” by 1.

while i < len(my_list):
    result += my_list[i]
    i += 1

Print the “result” variable.

print(result)
Sum All the Items in a List Using the while loop
Example-3: Sum All the Items in Python List Without using sum()

Read: Calculate the exponential value of a number in Python

Sum All the Items in Python List Using Recursion

Define a function called recursive_sum that takes two arguments: the list and the current index. Within the function, create a base case where if the current index is 0, return the item at that index in the list

READ:  Merge Dictionaries in Python (8 different methods)

Otherwise, return the item at the current index in the list plus the result of calling the recursive_sum function with the list and the next index

def recursive_sum(my_list, n):
    if n == 0:
        return my_list[n]
    else:
        return my_list[n] + recursive_sum(my_list, n-1)

Call the function with the list and the last index of the list.

my_list = [1, 2, 3, 4, 5]
result = recursive_sum(my_list, len(my_list)-1)

Print the result.

print(result)
Sum All the Items in a List Using the recursive function
Example-4: Sum All the Items in Python List Without using sum()

Read: How to Add Elements in a Set in Python

Sum All the Items in Python List Using the accumulate()

Import the accumulate() function from the itertools library.

from itertools import accumulate
my_list = [1, 2, 3, 4, 5]

Create a variable called result and set it equal to the last element of the accumulated list.

result = list(accumulate(my_list))[-1]

Print the result.

print(result)
Sum All the Items in a List Using the accumulate function
Example-5: Sum All the Items in Python List Without using sum()

Read: How to convert a dictionary into a string in Python

Sum All the Items in Python List Using List Comprehension

Initialize a variable called “result” to 0.

my_list = [1, 2, 3, 4, 5]
result = 0

Use a list comprehension to iterate through the list and add each item to the “result” variable.

result = sum(x for x in my_list)

Print the “result” variable.

print(result)
Sum All the Items in a List Using the list comprehension
Example-6: Sum All the Items in Python List Without using sum()

Read: How to Convert a list to DataFrame in Python

Sum All the Items in Python List Using Enumerate

Create a list named “list1” and assign it the values [1, 2, 3, 4, 5].

list1 = [1, 2, 3, 4, 5]

Initialize a variable s to 0. This variable will be used to store the sum of the elements in the list.

s=0

Use a for loop to iterate through the elements in “list1” using the “enumerate()” function. The “enumerate()” function adds a counter to an iterable and returns it in a form of enumerate object, which is an iterator containing pairs (index, element).

for i,a in enumerate(list1):

For each iteration of the loop, use the variable “i” to store the index of the current element and the variable “a” to store the current element.

s+=a 

Add the current value of “a” to the current value of “s” and assign the result back to “s”.

s+=a 

After all, iterations are complete, print the final value of “s” to the console. This will be the sum of all elements of the list.

print(s)
Sum All the Items in a List Using the enumeration.
Sum All the Items in a List Using the enumeration.
  • The while and for loop method is less efficient and requires a check inside the loop to see if we’ve reached the end of the list.
  • The recursive method consumes more memory because of the recursive call stack. The itertools.accumulate function is the most efficient method,
  • The list comprehension is the most readable.
READ:  How to save Python dictionary to CSV

You may also like to read the following Python tutorials.

Conclusion

In conclusion, there are multiple ways to sum all the items in Python list without using sum() function. We have discussed four different methods – using a while loop, recursion, itertools.accumulate function, enumerate and list comprehension. Each method has its own advantages and disadvantages.

  • Sum All the Items in Python List Using a For Loop
  • Sum All the Items in Python List Using the reduce()
  • Sum All the Items in Python List Using a While Loop
  • Sum All the Items in Python List Using Recursion
  • Sum All the Items in Python List Using the accumulate()
  • Sum All the Items in Python List Using List Comprehension
  • Sum All the Items in Python List Using Enumerate