Python – Summation of Unique elements
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))
|
Output
The 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))
|
Output
The 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))
|
Output
The 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))
|
Output
The 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))
|
Output
The 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)
|
Output
The 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))
|
Output
The 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.
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...