Python - Index match element Product Last Updated : 09 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Sometimes, while working with Python list we can have a problem in which we have to compare two lists for index similarity and hence can have a task of multiplication equal index pairs. Let’s discuss certain way in which this task can be performed. Method #1 : Using loop + zip() This task can be performed by passing the zip(), which performs task of mapping both list with each other, to the function which computes the product according to equal indices. Python3 # Python3 code to demonstrate working of # Index match element Product # using loop + zip() def prod(val) : res = 1 for ele in list(val): res *= ele return res # initialize lists test_list1 = [5, 6, 10, 4, 7, 1, 19] test_list2 = [6, 6, 10, 3, 7, 10, 19] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Index match element Product # using loop + zip() res = prod(x for x, y in zip(test_list1, test_list2) if x == y) # printing result print("Multiplication of Identical elements : " + str(res)) Output : The original list 1 is : [5, 6, 10, 4, 7, 1, 19] The original list 2 is : [6, 6, 10, 3, 7, 10, 19] Multiplication of Identical elements : 7980 Time complexity: O(M^N) as the number of combinations generated is M choose N.Auxiliary space: O(1) constant extra space is required Method #2 : Using reduce() Python3 import functools # initialize lists test_list1 = [5, 6, 10, 4, 7, 1, 19] test_list2 = [6, 6, 10, 3, 7, 10, 19] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Index match element Product # using functools.reduce() res = 1 res = functools.reduce((lambda x, y: x * y), [x for x, y in zip(test_list1, test_list2) if x == y]) # printing result print("Multiplication of Identical elements : " + str(res)) #This code is contributed by Edula Vinay Kumar Reddy OutputThe original list 1 is : [5, 6, 10, 4, 7, 1, 19] The original list 2 is : [6, 6, 10, 3, 7, 10, 19] Multiplication of Identical elements : 7980 Time complexity: o(n) Space complexity: o(n) Comment More infoAdvertise with us Next Article Python - Elements Product till K value M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python - Product of elements using Index list Accessing an element from its index is easier task in python, just using the [] operator in a list does the trick. But in certain situations we are presented with tasks when we have more than once indices and we need to get all the elements corresponding to those indices and then perform the multipl 7 min read Python - Get match indices Getting match indices in Python involves identifying the positions of specific substrings or patterns within a string. It helps in locating where a certain sequence occurs, facilitating further operations based on those positions. In this article, we will see How to match indices in a list. Using Di 2 min read Python - Kth Column Product in Tuple List Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the product of itâs Kth index. This problem has application in web development domain while working with data informations. Letâs discuss certain ways in which this task can be performed. M 7 min read Python - Elements Product till K value One of the problem that is basically a subproblem for many complex problems, finding product number till a certain number in list in python, is commonly encountered and this particular article discusses possible solutions to this particular problem. Method #1 : Naive method The most common way this 5 min read Python | Record Index Product Sometimes, while working with records, we can have a problem in which we need to multiply all the columns of a container of lists that are tuples. This kind of application is common in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + li 4 min read Search Elements in a Matrix - Python The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1 3 min read Like