Python – Summation of Unique elements
Last Updated :
25 Apr, 2025
This article focuses on one of the operation of getting the unique list from a list that contains a possible duplicates and performing its summation. This operations has large no. of applications and hence it’s knowledge is good to have.
Method 1 : Naive method + sum()
In naive method, we simply traverse the list and append the first occurrence of the element in new list and ignore all the other occurrences of that particular element. The task of summation is performed using sum().
Python3
test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
if i not in res:
res.append(i)
res = sum (res)
print ( "The unique elements summation : " + str (res))
|
OutputThe original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements summation : 15
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method 2 : Using set() + sum()
This is the most popular way by which the duplicated are removed from the list. After that the summation of list can be performed using sum().
Python3
test_list = [ 1 , 5 , 3 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
res = sum ( list ( set (test_list)))
print ( "The unique elements summation : " + str (res))
|
OutputThe original list is : [1, 5, 3, 6, 3, 5, 6, 1]
The unique elements summation : 15
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n).
Method #3:Using Counter() function
Python3
from collections import Counter
test_list = [ 1 , 5 , 3 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
freq = Counter(test_list)
res = sum (freq.keys())
print ( "The unique elements summation : " + str (res))
|
OutputThe original list is : [1, 5, 3, 6, 3, 5, 6, 1]
The unique elements summation : 15
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4:Using Operator.countOf() method
Python3
import operator as op
test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
if op.countOf(res,i) = = 0 :
res.append(i)
res = sum (res)
print ( "The unique elements summation : " + str (res))
|
OutputThe original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements summation : 15
Time Complexity:O(N)
Auxiliary Space:O(N)
Method #5:Using numpy method
Python3
import numpy as np
test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
res = np. sum (np.unique(test_list))
print ( "The unique elements summation : " + str (res))
|
OutputThe original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements summation : 15
Time Complexity:O(N)
Auxiliary Space:O(N)
Method #6: Using List Comprehension
Python3
test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " , test_list)
res = sum ([i for i in set (test_list) if i < = 6 ])
print ( "The unique elements summation : " , res)
|
OutputThe original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements summation : 15
Time Complexity:O(N)
Auxiliary Space:O(N)
Method 7 : use the built-in function reduce() from the functools module
Approach:
- Import the reduce() function from the functools module.
- Initialize a set and a variable sum to 0.
- Use the reduce() function to iterate through the list and add the unique elements to the set while also adding them to the sum.
- After the loop, the sum variable will hold the sum of unique elements.
Python3
from functools import reduce
test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
res = reduce ( lambda acc, x: (acc[ 0 ] + x, acc[ 1 ] | {x}) if x not in acc[ 1 ] else acc, test_list, ( 0 , set ()))[ 0 ]
print ( "The unique elements summation : " + str (res))
|
OutputThe original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements summation : 15
Time complexity: O(nlogn) due to the use of reduce()
Auxiliary space: O(n) due to the use of a set to store unique elements.
Similar Reads
Python - Sum of tuple elements
Sometimes, while programming, we have a problem in which we might need to perform summation among tuple elements. This is an essential utility as we come across summation operations many times and tuples are immutable and hence required to be dealt with. Letâs discuss certain ways in which this task
6 min read
Python | Pair summation of list elements
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the summation of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let's discuss certain ways in which this problem can be solve
4 min read
Python | Column summation of tuples
Sometimes, we encounter a problem where we deal with a complex type of matrix column summation in which we are given a tuple and we need to perform the summation of its like elements. This has a good application in Machine Learning domain. Let's discuss certain ways in which this can be done. Method
7 min read
Python | Above K elements summation
Many times we might have problem in which we need to find summation rather than the actual numbers and more often, the result is conditioned.. Letâs discuss certain ways in which this problem can be successfully solved. Method #1 : Using loop This problem can easily be solved using loop with a brute
3 min read
Element indices Summation - Python
Our task is to calculate the sum of elements at specific indices in a list. This means selecting elements at specific positions and computing their sum. Given a list and a set of indices, the goal is to compute the sum of elements present at those indices. For example, given the list [10, 20, 30, 40
3 min read
Python - First K unique elements
Sometimes, while working with Python Lists, we can have a problem in which we need to extract first K unique elements. This means we need to extract duplicate if they occur in first K elements as well. This can essentially make count of first K unique elements more than K. This kind of problem can h
3 min read
Python | Summation of tuples in list
Sometimes, while working with records, we can have a problem in which we need to find the cumulative sum of all the values that are present in tuples. This can have applications in cases in which we deal with a lot of record data. Let's discuss certain ways in which this problem can be solved. Metho
7 min read
Python | Summation of Kth Column of Tuple List
Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the possible accumulation of its Kth index. This problem has applications in the web development domain while working with data information. Let's discuss certain ways in which this task ca
7 min read
Python | Grouped summation of tuple list
Many times, we are given a list of tuples and we need to group its keys and perform certain operations while grouping. The most common operation is addition. Let's discuss certain ways in which this task can be performed. Apart from addition, other operations can also be performed by doing small cha
10 min read
Python | Common Row elements Summation
The problem of finding the common elements in the list of 2 lists is quite a common problem and can be dealt with with ease and also has been discussed many times. But sometimes, we require to find the elements that are in common with N lists and return their sum. Let us discuss certain ways in whic
6 min read