python | Nested List Intersection Matrix Product
Last Updated :
17 Apr, 2023
The problem of finding the common elements in list of 2 lists is quite a common problem and can be dealt with ease and also has been discussed before many times. But sometimes, we require to find the elements that are in common from N lists and return their product. Let’s discuss certain ways in which this operation can be performed.
Method #1 : Using reduce() + lambda + set() + loop This particular task can be achieved in just a one line using the combination of the above functions. The reduce function can be used to operate the function of “&” operation to all the list. The set function can be used to convert list into a set to remove repetition. The task of performing product is done using loop.
Python3
# Python code to demonstrate
# Nested List Intersection Matrix Product
# using reduce() + lambda + set() + loop
# getting Product
def prod(val) :
res = 1
for ele in val:
res *= ele
return res
# initializing list of lists
test_list = [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
# printing original list
print ("The original list is : " + str(test_list))
# Nested List Intersection Matrix Product
# using reduce() + lambda + set() + loop
res = prod(list(reduce(lambda i, j: i & j, (set(x) for x in test_list))))
# printing result
print ("The common row elements product is : " + str(res))
Output : The original list is : [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
The common row elements product is : 6
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required
Method #2 : Using map() + intersection() + loop The map function can be used to convert each of the lists to set to be operated by to perform the intersection, using the set.intersection function. This is the most elegant way to perform this particular task. The task of performing product is done using loop.
Python3
# Python3 code to demonstrate
# Nested List Intersection Matrix Product
# using map() + intersection() + loop
# getting Product
def prod(val) :
res = 1
for ele in val:
res *= ele
return res
# initializing list of lists
test_list = [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
# printing original list
print ("The original list is : " + str(test_list))
# Common Row elements Summation
# using map() + intersection() + loop
res = prod(list(set.intersection(*map(set, test_list))))
# printing result
print ("The common row elements product is : " + str(res))
Output : The original list is : [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
The common row elements product is : 6
Time Complexity: O(n*n) where n is the number of elements in the test list. The loop is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(1) constant additional space needed
Similar Reads
Python | Intersection of multiple lists Given two list of lists, write a Python program to find the intersection between the given two lists. Examples: Input : lst1 = [['a', 'c'], ['d', 'e']] lst2 = [['a', 'c'], ['e', 'f'], ['d', 'e']] Output : [['a', 'c'], ['d', 'e']] Input : lst1 = [[1, 5, 7], [2, 3], [6, 9], [4, 8]] lst2 = [[9, 3], [2,
5 min read
Python | Nth column Matrix Product Sometimes, while working with Python Matrix, we may have a problem in which we require to find the product of a particular column. This can have a possible application in day-day programming and competitive programming. Letâs discuss certain ways in which this task can be performed. Method #1: Using
7 min read
Python | Product of Prefix in list Nowadays, especially in competitive programming, the utility of computing prefix product is quite popular and features in many problems. Hence, having a one-liner solution to it would possess a great help. Letâs discuss certain ways in which this problem can be solved. Method 1: Using list comprehen
4 min read
Python | Column Product in List of lists Sometimes, we are encountered with such problem in which we need to find the product of each column in a matrix i.e product of each index in list of lists. This kind of problem is quite common and useful in competitive programming. Letâs discuss certain ways in which this problem can be solved. Meth
6 min read
Python | Pair Product combinations Sometimes, while working with data, we can have a problem in which we need to perform tuple multiplication among all the tuples in list. This can have application in many domains. Letâs discuss certain ways in which this task can be performed. Method #1 : Using combinations() + list comprehension Th
4 min read
Python - Tuple List intersection (Order irrespective) Given list of tuples, perform tuple intersection of elements irrespective of their order. Input : test_list1 = [(3, 4), (5, 6)], test_list2 = [(5, 4), (4, 3)] Output : {(3, 4)} Explanation : (3, 4) and (4, 3) are common, hence intersection ( order irrespective). Input : test_list1 = [(3, 4), (5, 6)]
6 min read