 Open in App
Not now

# Python – Summation of Unique elements

• Last Updated : 27 Mar, 2023

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))`

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

 `# 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))`

Output

```The original list is : [1, 5, 3, 6, 3, 5, 6, 1]
The unique elements summation : 15```

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))`

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

 `# 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))`

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

 `#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))` `#using numpy``#Summation of Unique elements``#from list``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`

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)``#This code is contributed by Jyothi pinjala.`

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)

My Personal Notes arrow_drop_up