Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – Unique values Multiplication

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
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res 
 
# initializing list
test_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 = []
for i in test_list:
    if i not in res:
        res.append(i)
res = prod(res)
 
# printing list after product
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




# Python 3 code to demonstrate
# Unique values Multiplication
# using set() + loop
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res 
 
# initializing list
test_list = [1, 5, 3, 6, 3, 5, 6, 1]
print ("The original list is : " + str(test_list))
 
# Unique values Multiplication
# using set() + loop
res = prod(list(set(test_list)))
 
# Unique values Multiplication
# using set() + loop
# printing result
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




# Python 3 code to demonstrate
# Unique values Multiplication
from collections import Counter
# getting Product
 
 
def prod(val):
    res = 1
    for ele in val:
        res *= ele
    return res
 
 
# initializing list
test_list = [1, 5, 3, 6, 3, 5, 6, 1]
print("The original list is : " + str(test_list))
freq = Counter(test_list)
# Unique values Multiplication
uniqueValues = freq.keys()
res = prod(uniqueValues)
 
# Unique values Multiplication
# printing result
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




# Python 3 code to demonstrate
# Unique values Multiplication
import operator as op
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res 
 
# initializing list
test_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 = []
for i in test_list:
    if op.countOf(res,i)==0:
        res.append(i)
res = prod(res)
 
# printing list after product
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




#Using numpy approach
#Python 3 code to demonstrate
#Unique values Multiplication
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 approach
#Unique values Multiplication
unique_list = np.unique(test_list)
result = np.prod(unique_list)
 
#printing list after product
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)
# initializing list
test_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 product
unique_list = list(set(test_list))
result = prod(unique_list)
# printing result
print ("The unique elements product : " + str(result))
#This code is contributed by Jyothi pinjala


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
 
# initializing list
test_list = [1, 3, 5, 6, 3, 5, 6, 1]
print ("The original list is : " + str(test_list))
 
# using groupby function from itertools library
unique_values = [key for key, group in itertools.groupby(sorted(test_list))]
 
# getting the product of the unique values
result = 1
for value in unique_values:
    result *= value
 
# printing the result
print("The unique values multiplication is:", result)
#This code is contributed by Vinay Pinjala.


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)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments