Python Exercises: Sum of all list elements except current element
Python List: Exercise - 275 with Solution
Write a Python program to add all elements of a list of integers except the number at index. Return the updated string.
Example: ([1, 2, 3]) -> [1+2+3-1, 1+2+3-2, 1+2+3-3]
-> [5, 4, 3].
Sample Data:
([0, 9, 2, 4, 5, 6] -> [26, 17, 24, 22, 21, 20]
([-4, 0, 6, 1, 0, 2]) -> [9, 5, -1, 4, 5, 3]
([1, 2, 3]) -> [5, 4, 3]
([-4, 0, 5, 1, 0, 1]) -> [7, 3, -2, 2, 3, 2]
Sample Solution-1:
Python Code:
def test(nums):
return [sum(nums)-(x) for x in nums]
nums = [0, 9, 2, 4, 5, 6]
print("Original list:")
print(nums)
print("Sum of said list elements except current element:\n", test(nums))
nums = [-4, 0, 6, 1, 0, 2]
print("\nOriginal list:")
print(nums)
print("Sum of said list elements except current element:\n", test(nums))
nums = [1, 2, 3]
print("\nOriginal list:")
print(nums)
print("Sum of said list elements except current element:\n", test(nums))
nums = [-4, 0, 5, 1, 0, 1]
print("\nOriginal list:")
print(nums)
print("Sum of said list elements except current element:\n", test(nums))
Sample Output:
Original list: [0, 9, 2, 4, 5, 6] Sum of said list elements except current element: [26, 17, 24, 22, 21, 20] Original list: [-4, 0, 6, 1, 0, 2] Sum of said list elements except current element: [9, 5, -1, 4, 5, 3] Original list: [1, 2, 3] Sum of said list elements except current element: [5, 4, 3] Original list: [-4, 0, 5, 1, 0, 1] Sum of said list elements except current element: [7, 3, -2, 2, 3, 2]
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Sample Solution-2:
Python Code:
def test(nums):
result = []
for x in range(len(nums)):
n = sum(nums[x+1:] + nums[:x])
print(n)
result.append(n)
return result
nums = [0, 9, 2, 4, 5, 6]
print("Original list:")
print(nums)
print("Sum of said list elements except current element:\n", test(nums))
nums = [-4, 0, 6, 1, 0, 2]
print("\nOriginal list:")
print(nums)
print("Sum of said list elements except current element:\n", test(nums))
nums = [1, 2, 3]
print("\nOriginal list:")
print(nums)
print("Sum of said list elements except current element:\n", test(nums))
nums = [-4, 0, 5, 1, 0, 1]
print("\nOriginal list:")
print(nums)
print("Sum of said list elements except current element:\n", test(nums))
Sample Output:
Original list: [0, 9, 2, 4, 5, 6] 26 17 24 22 21 20 Sum of said list elements except current element: [26, 17, 24, 22, 21, 20] Original list: [-4, 0, 6, 1, 0, 2] 9 5 -1 4 5 3 Sum of said list elements except current element: [9, 5, -1, 4, 5, 3] Original list: [1, 2, 3] 5 4 3 Sum of said list elements except current element: [5, 4, 3] Original list: [-4, 0, 5, 1, 0, 1] 7 3 -2 2 3 2 Sum of said list elements except current element: [7, 3, -2, 2, 3, 2]
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous Python Exercise: Count lowercase letters in a list of words.
Next Python Exercise: Find the largest odd number in a list of integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Returns a list of elements that exist in both lists, after applying the provided function to each list element of both.
Example:
def tips_intersection_by(a, b, fn): _b = set(map(fn, b)) return [item for item in a if fn(item) in _b] from math import floor print(tips_intersection_by([2.1, 1.2], [2.3, 3.4],floor))
Output:
[2.1]
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises