Python - Index Match Element Product

The Index Match Element Product refers to finding elements at the same positions in two lists that have equal values, then calculating their product. For example, if two lists have matching elements at positions 0 and 2, we multiply those matched elements together.

Let's understand this with an example ?

list_1 = [10, 20, 30, 40]
list_2 = [10, 29, 30, 10]

print("List 1:", list_1)
print("List 2:", list_2)
print("Matches at index 0: 10 == 10")
print("Matches at index 2: 30 == 30")
print("Product: 10 * 30 = 300")
List 1: [10, 20, 30, 40]
List 2: [10, 29, 30, 10]
Matches at index 0: 10 == 10
Matches at index 2: 30 == 30
Product: 10 * 30 = 300

Using For Loop

The simplest approach uses a for loop to iterate through both lists and check for matching elements at each index ?

list1 = [10, 20, 30, 40, 50, 60]
list2 = [10, 34, 3, 89, 7, 60]

product = 1
matches_found = False

for i in range(len(list1)):
    if list1[i] == list2[i]:
        product *= list1[i]
        matches_found = True
        print(f"Match at index {i}: {list1[i]}")

if matches_found:
    print("Result of index match element product:", product)
else:
    print("No matches found")
Match at index 0: 10
Match at index 5: 60
Result of index match element product: 600

Using List Comprehension and zip()

List comprehension with zip() provides a more concise approach by combining both lists element-wise ?

list1 = [10, 20, 30, 40, 50, 60]
list2 = [10, 34, 30, 89, 7, 60]

# Find matching elements using list comprehension
matching_elements = [x for x, y in zip(list1, list2) if x == y]
print("Matching elements:", matching_elements)

# Calculate product
product = 1
for element in matching_elements:
    product *= element

print("Result of index match element product:", product)
Matching elements: [10, 30, 60]
Result of index match element product: 18000

Using NumPy

NumPy arrays offer efficient element-wise comparison and product calculation ?

import numpy as np

list1 = [10, 20, 30, 40, 50, 60]
list2 = [10, 34, 30, 89, 50, 6]

# Convert lists to arrays
arr1 = np.array(list1)
arr2 = np.array(list2)

# Find matching elements
matching_elements = arr1[arr1 == arr2]
print("Matching elements:", matching_elements)

# Calculate product using numpy
product = np.prod(matching_elements) if len(matching_elements) > 0 else 1

print("Result of index match element product:", product)
Matching elements: [10 30 50]
Result of index match element product: 15000

Using functools.reduce()

The reduce() function applies a function cumulatively to calculate the product of matching elements ?

from functools import reduce

list1 = [10, 20, 30, 40, 7, 60]
list2 = [10, 34, 30, 89, 7, 60]

# Find matching elements
matching_elements = [x for x, y in zip(list1, list2) if x == y]
print("Matching elements:", matching_elements)

# Calculate product using reduce and lambda
product = reduce(lambda x, y: x * y, matching_elements) if matching_elements else 1

print("Result of index match element product:", product)
Matching elements: [10, 30, 7, 60]
Result of index match element product: 126000

Comparison

Method Best For Memory Usage Performance
For Loop Simple cases, learning Low Good for small lists
List Comprehension Readable, Pythonic code Medium Good
NumPy Large datasets, numerical computing Efficient Best for large arrays
functools.reduce() Functional programming style Low Good

Conclusion

Index match element product finds common elements at the same positions in two lists and multiplies them together. Use for loops for simplicity, NumPy for large datasets, or list comprehension for readable Python code.

Updated on: 2026-03-27T12:41:15+05:30

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements