List Product Excluding Duplicates - Python
Last Updated :
10 Feb, 2025
The task of finding the product of unique elements in a list involves identifying and multiplying only the distinct values, effectively excluding any duplicates. For example, if a = [1, 3, 5, 6, 3, 5, 6, 1], the unique elements would be {1, 3, 5, 6}, and the product of these values would be 90.
Using set()
set() automatically removes duplicates because sets do not allow duplicate elements. By converting the list to a set, we can efficiently filter out repeated values and then compute the product of the unique elements. This approach is simple and works well for most cases.
Python
a = [1, 3, 5, 6, 3, 5, 6, 1]
# Unique Elements
b = set(a) # Convert `a` to set
res = 1 # initialize res to 1
for i in b:
res *= i
print(str(res))
Explanation: for loop iterates through each element in the set unique_elements. During each iteration, the value of res is multiplied by the current element i , updating the product. The loop computes the product as 1 * 3 * 5 * 6, which results in 90.
Using dict.fromkeys()
dict.fromkeys() creates a dictionary with list elements as keys, which inherently removes any duplicates. This method is straightforward and easy to understand, offering a readable way to filter out duplicates and calculate the product of unique values while maintaining the order of insertion.
Python
a = [1, 3, 5, 6, 3, 5, 6, 1]
# Unique Elements
b = dict.fromkeys(a) # Remove duplicates using dictionary keys
res = 1 # initialize res to 1
for i in unique_val:
res *= i
print(str(res))
Explanation :for loop iterates over the keys of the dictionary and multiplies res by each key. The product is calculated as 1 * 3 * 5 * 6, which gives 90.
Using Counter
Counter class from the collections module is typically used for counting occurrences of elements. By extracting the keys from the Counter object, we can get the unique elements. This method is helpful when we also need frequency information, though it can be used just to filter duplicates and compute the product of the unique numbers.
Python
from collections import Counter
a = [1, 3, 5, 6, 3, 5, 6, 1]
# Unique Values
b = Counter(a) # Count frequency of elements
res = 1 # initialize res to 1
for i in b:
res *= i
print(str(res))
Explanation: for loop iterates through the keys of the Counter object. In each iteration, res is multiplied by the current element. The product is calculated as 1 * 3 * 5 * 6, resulting in 90.
Using in keyword
In this approach, a for loop iterates through the list and the in keyword is used to check if an element has already been added to a separate list of unique elements. This method is easy to implement but can become slower with larger lists due to the repeated checks.
Python
a = [1, 3, 5, 6, 3, 5, 6, 1]
# Unique Elements
b = []
for i in a:
if i not in b:
b.append(i)
res = 1 # initialize res to 1
for i in b:
res *= i
print(str(res))
Explanation: for loop iterates over each element in the list a, appending it to unique_elements if it's not already present. This ensures only unique elements are stored. Another for loop iterates through unique_elements, multiplying res by each element.
Similar Reads
Python - Difference of Two Lists Including Duplicates We are given two list we need find difference between two list. For example, a = [1, 2, 2, 3, 4] and b = [2, 3] we need to find difference of two list so that resultant output should be [1, 2, 4].Using a for loopA for loop can iterate through first list and remove matching elements from second list
2 min read
Python - Merging duplicates to list of list We are given a list we need to merge the duplicate to lists of list. For example a list a=[1,2,2,3,4,4,4,5] we need to merge all the duplicates in lists so that the output should be [[2,2],[4,4,4]]. This can be done by using multiple functions like defaultdict from collections and Counter and variou
3 min read
Python - Difference of List keeping duplicates The problem of finding difference between list, i.e removing elements that occur in one list and not in other is discussed before. But the usage of sets ignores duplicates and we sometimes, require to remove the exact elements that occur in lists. Lets discuss certain ways in which this task can be
6 min read
How to Find Duplicates in a List - Python Finding duplicates in a list is a common task in programming. In Python, there are several ways to do this. Letâs explore the efficient methods to find duplicates. Using a Set (Most Efficient for Large Lists)Set() method is used to set a track seen elements and helps to identify duplicates. Pythona
2 min read
Python - Duplicate Element Indices in List We are having a list we need to find the duplicate element indices. For example, we are given a list a = [10, 20, 30, 20, 40, 30, 50] we need to find indices of the duplicate items so that output should be {20: [1, 3], 30: [2, 5]}.Using a loop and a dictionaryWe iterate through the list using enumer
3 min read