Python – Unique values Multiplication
This article focuses on one of the operation of getting the unique list from a list that contains a possible duplicates and performing its product. This operations has large no. of applications and hence it’s knowledge is good to have.
Method 1 : Naive method + loop 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 product is performed using loop.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
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 = prod(res)
print ("The unique elements product : " + str (res))
|
Output :
The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements product : 90
Time Complexity: O(n*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() + loop This is the most popular way by which the duplicated are removed from the list. After that the product of list can be performed using loop.
Python3
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
test_list = [ 1 , 5 , 3 , 6 , 3 , 5 , 6 , 1 ]
print ("The original list is : " + str (test_list))
res = prod( list ( set (test_list)))
print ("The unique elements product : " + str (res))
|
Output :
The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements product : 90
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space required
Method #3:Using Counter() function
Python3
from collections import Counter
def prod(val):
res = 1
for ele in val:
res * = ele
return res
test_list = [ 1 , 5 , 3 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
freq = Counter(test_list)
uniqueValues = freq.keys()
res = prod(uniqueValues)
print ( "The unique elements product : " + str (res))
|
Output
The original list is : [1, 5, 3, 6, 3, 5, 6, 1]
The unique elements product : 90
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #4:Using Operator.countOf() method
Python3
import operator as op
def prod(val) :
res = 1
for ele in val:
res * = ele
return res
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 = prod(res)
print ( "The unique elements product : " + str (res))
|
Output
The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements product : 90
Auxiliary Space: O(N)
Time Complexity:O(N)
Method 5 : Using numpy
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np
test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
unique_list = np.unique(test_list)
result = np.prod(unique_list)
print ( "The unique elements product : " + str (result))
|
Output:
The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements product : 90
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 6 : Using List comprehension:
Python3
from functools import reduce
def prod(val) :
if not val:
return 1
return reduce ( lambda x, y: x * y, val)
test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
unique_list = list ( set (test_list))
result = prod(unique_list)
print ( "The unique elements product : " + str (result))
|
Output
The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique elements product : 90
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 7: Using itertools groupby()
Python3
import itertools
test_list = [ 1 , 3 , 5 , 6 , 3 , 5 , 6 , 1 ]
print ( "The original list is : " + str (test_list))
unique_values = [key for key, group in itertools.groupby( sorted (test_list))]
result = 1
for value in unique_values:
result * = value
print ( "The unique values multiplication is:" , result)
|
Output
The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The unique values multiplication is: 90
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
09 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...