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
| # Python 3 code to demonstrate # Unique values Multiplication# using naive methods + loop# getting Product defprod(val) :     res =1    forele inval:         res *=ele     returnres  # initializing listtest_list =[1, 3, 5, 6, 3, 5, 6, 1]print("The original listis: " +str(test_list))# using naive method + loop# Unique values Multiplication# from list res =[]fori intest_list:    ifi notinres:        res.append(i)res =prod(res)# printing list after productprint("The unique elements product : " +str(res)) | 
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
| # Python 3 code to demonstrate # Unique values Multiplication# using set() + loop# getting Product defprod(val) :     res =1    forele inval:         res *=ele     returnres  # initializing listtest_list =[1, 5, 3, 6, 3, 5, 6, 1]print("The original listis: " +str(test_list))# Unique values Multiplication# using set() + loop res =prod(list(set(test_list)))# Unique values Multiplication# using set() + loop# printing resultprint("The unique elements product : " +str(res)) | 
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
| # Python 3 code to demonstrate# Unique values Multiplicationfromcollections importCounter# getting Productdefprod(val):    res =1    forele inval:        res *=ele    returnres# initializing listtest_list =[1, 5, 3, 6, 3, 5, 6, 1]print("The original list is : "+str(test_list))freq =Counter(test_list)# Unique values MultiplicationuniqueValues =freq.keys()res =prod(uniqueValues)# Unique values Multiplication# printing resultprint("The unique elements product : "+str(res)) | 
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
| # Python 3 code to demonstrate # Unique values Multiplicationimportoperator as op# getting Product defprod(val) :     res =1    forele inval:         res *=ele     returnres  # initializing listtest_list =[1, 3, 5, 6, 3, 5, 6, 1]print("The original list is : "+str(test_list))# using naive method + loop# Unique values Multiplication# from list res =[]fori intest_list:    ifop.countOf(res,i)==0:        res.append(i)res =prod(res)# printing list after productprint("The unique elements product : "+str(res)) | 
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
| #Using numpy approach#Python 3 code to demonstrate#Unique values Multiplicationimportnumpy as np#initializing listtest_list =[1, 3, 5, 6, 3, 5, 6, 1]print("The original list is : "+str(test_list))#using numpy approach#Unique values Multiplicationunique_list =np.unique(test_list)result =np.prod(unique_list)#printing list after productprint("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
| fromfunctools importreducedefprod(val) :     ifnotval:        return1    returnreduce(lambdax, y: x *y, val)# initializing listtest_list =[1, 3, 5, 6, 3, 5, 6, 1]print("The original list is : "+str(test_list))# using set to get unique values# and then using list comprehension to calculate productunique_list =list(set(test_list))result =prod(unique_list)# printing resultprint("The unique elements product : "+str(result))#This code is contributed by Jyothi pinjala | 
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
| importitertools# initializing listtest_list =[1, 3, 5, 6, 3, 5, 6, 1]print("The original list is : "+str(test_list))# using groupby function from itertools libraryunique_values =[key forkey, group initertools.groupby(sorted(test_list))]# getting the product of the unique valuesresult =1forvalue inunique_values:    result *=value# printing the resultprint("The unique values multiplication is:", result)#This code is contributed by Vinay Pinjala. | 
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)
 
				 
					


