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
# Python 3 code to demonstrate # Summation of Unique elements # using naive methods + sum() # initializing list test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ] print ( "The original list is : " + str (test_list)) # using naive method + sum() # Summation of Unique elements # from list res = [] for i in test_list: if i not in res: res.append(i) res = sum (res) # printing list after removal print ( "The unique elements summation : " + str (res)) |
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
# Python 3 code to demonstrate # Summation of Unique elements # using set() + sum() # initializing list test_list = [ 1 , 5 , 3 , 6 , 3 , 5 , 6 , 1 ] print ( "The original list is : " + str (test_list)) # using set() + sum() # Summation of Unique elements # from list res = sum ( list ( set (test_list))) # Summation of Unique elements # using set() + sum() print ( "The unique elements summation : " + str (res)) |
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
# Python 3 code to demonstrate # Summation of Unique elements from collections import Counter # initializing list 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()) # Summation of Unique elements print ( "The unique elements summation : " + str (res)) |
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
# Python 3 code to demonstrate # Summation of Unique elements import operator as op # initializing list test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ] print ( "The original list is : " + str (test_list)) # using naive method + sum() # Summation of Unique elements # from list res = [] for i in test_list: if op.countOf(res,i) = = 0 : res.append(i) res = sum (res) # printing list after removal print ( "The unique elements summation : " + str (res)) |
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
#Python 3 code to demonstrate #Summation of Unique elements #using numpy import numpy as np # Initializing list test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ] print ( "The original list is : " + str (test_list)) #Summation of Unique elements #from list #using numpy res = np. sum (np.unique(test_list)) # Printing result print ( "The unique elements summation : " + str (res)) # This code is contributed by Edula Vinay Kumar Reddy |
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
# Initializing list 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) # This code is contributed by Jyothi pinjala. |
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 # initializing list test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ] print ( "The original list is : " + str (test_list)) # using reduce() method # Summation of Unique elements # from 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 ] # printing list after removal print ( "The unique elements summation : " + str (res)) |
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.