Skip to content
Related Articles

Related Articles

Python – List product excluding duplicates

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 12 Aug, 2022
View Discussion
Improve Article
Save Article

This article focuses on one of the operations of getting the unique list from a list that contains a possible duplicated and finding its product. This operation has a large no. of applications and hence its knowledge is good to have.

Method 1: Naive method 

In the 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 performing the product is done using loop.

Python3




# Python 3 code to demonstrate
# Duplication Removal List Product
# using naive methods
 
# 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
# Duplication Removal List Product
res = []
for i in test_list:
    if i not in res:
        res.append(i)
res = prod(res)
 
# printing list after removal
print ("Duplication removal list product : " + str(res))

Output : 

The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
Duplication removal list product : 90

 

 
Method 2: Using list comprehension 

This method has working similarly to the above method, but this is just a one-liner shorthand for a longer method done with the help of list comprehension. 

Python3




# Python 3 code to demonstrate
# Duplication Removal List Product
# using list comprehension
 
# 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 list comprehension
# Duplication Removal List Product
res = []
[res.append(x) for x in test_list if x not in res]
res = prod(res)
 
# printing list after removal
print ("Duplication removal list product : " + str(res))

Output : 

The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
Duplication removal list product : 90

 

Method 3: Using set() and functools.reduce() [Intermediate]

Checking for list membership is O(n) on average, so building up the list of non-duplicates becomes an O(n2) operation on average. One can transform a list into a set to remove the duplicates which is O(n) complexity.  After this the product of the set elements can be calculated in the usual fashion by iterating over them or, to further reduce the code, the reduce() method from functools module can be used. As the documentation explains:

Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.

Here’s how the code can be reduced to a couple of lines

Python3




import functools
 
functools.reduce(lambda x, y: x*y, set([1, 3, 5, 6, 3, 5, 6, 1]), 1)

What this does is applies a lambda to the set obtained from the list. The lambda takes in two arguments x, and y and returns the product of the two numbers. This lambda is applied to all the elements in the list cumulatively i.e.

lambda(lambda(lambda(1, 3), 5), 6)….

which yields the final product as 90. It’s always prudent to add in an initial argument to the reduce() function to handle empty sequences so that the default value can be returned. In this case, since it’s a product, that value should be 1. Thus something like

functools.reduce(lambda x,y: x*y, set([]), 1)

yields the output 1, despite the list being empty. This initial argument is added before the values in the sequence and the lambda is applied as usual.

lambda(lambda(lambda(lambda(1,1 ), 3), 5), 6)….

Method 4: Using set() to remove duplicates and for loop to find product

Python3




# Python 3 code to demonstrate
# Duplication Removal List Product
 
test_list = [1, 3, 5, 6, 3, 5, 6, 1]
print ("The original list is : " + str(test_list))
 
# using naive method
# Duplication Removal List Product
x=list(set(test_list))
prod=1
for i in x:
    prod*=i
 
# printing list after removal
print ("Duplication removal list product : " + str(prod))

Output

The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
Duplication removal list product : 90

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!