
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Index Match Element Product in Python
The Index match element Product refers to two different lists where it contains the elements and set to respective variable. If common matches are found, then it filters the element by multiplication. To solve this problem statement, Python has some built-in functions such as range(), len(), zip(), prod(), reduce(), and, lambda().
Let's take an example of this.
The given input lists:
list_1 = [10, 20, 30, 40]
list_2 = [10, 29, 30, 10]
So, the common matches found in indexes 0 and 2 and its product become 10*30 = 300.
Syntax
The following syntax is used in the examples
range()
The range() is an in-built function in Python that returns the sequence of numbers according to a given range.
len()
The len() is an in-built function in Python that returns the length of the objects.
zip()
The built-in function
prod()
The prod() is an in-built function in Python that returns the product of all the elements.
reduce()
The reduce is a built-in method in Python that follows the functools module to return a single value by accepting two parameters- function and iterable.
lambda()
The function lambda offers a shortcut for declaring brief anonymous functions using the lambda keyword. The lambda function only has one expression.
Using for loop
In the following example, we will start the program by creating two lists in the respective variable. Then set the initial value p to 1 which will be used for products with a list element. Next, use the for loop to iterate through the first input list by using built-in function len(). Using the if statement it checks whether the first list of indexes matches to the second list of indexes or not. If common matches are found then filter those list elements by multiply 1 which simplifies the output.
Example
lst1 = [10, 20, 30, 40, 50, 60] lst2 = [10, 34, 3, 89, 7, 60] p = 1 for i in range(len(lst1)): if lst1[i] == lst2[i]: p *= lst1[i] print("Result of index match element Product:\n", p)
Output
Result of index match element Product: 600
Using list Comprehension and zip() Function
In the following example, the program uses list comprehension where zip() method combines two different lists together to find common matches. If the common element matches from two lists then it returns the filter result by doing product of those elements.
Example
# Using list comprehension and zip() lst1 = [10, 20, 30, 40, 50, 60] lst2 = [10, 34, 30, 89, 7, 60] # Initialize the initial value of the Product product = 1 match_item = [x for x, y in zip(lst1, lst2) if x == y] # Using if-statement to iterate the matching element if match_item: product = 1 for element in match_item: product *= element print("Result of index match element Product:\n", product)
Output
Result of index match element Product: 18000
Using Numpy Library
In the following example, the program uses numpy library and the object reference as np. Then create the two lists that will be used to find the common index element. Next, convert the list into the array in the respective variable which will be used as an array of elements. Now find the common index element from both the array and using prod() multiply all the common elements and display the result.
Example
import numpy as np # create the list lst1 = [10, 20, 30, 40, 50, 60] lst2 = [10, 34, 30, 89, 50, 6] # Convert the list into an array arr1 = np.array(lst1) arr2 = np.array(lst2) # Set the condition for matching element match_item = arr1[arr1 == arr2] prod_idx = np.prod(match_item) # Display the result print("Result of index match element Product:\n", prod_idx)
Output
Result of index match element Product: 15000
Using Functools Library
pip install functools
The above required command is necessary to install on the system to run the specific program based on functools.
In the following example, start the program with functools library and define the module named reduce that will calculate the index matching element. Then use the list comprehension where zip() function contains two lists to set for equivalence condition to find the common matches. Next, using some built-in functions- reduce(), lambda, and if-set to set the condition based on product of the matches element. Finally, display the result.
Example
from functools import reduce # Create the list lst1 = [10, 20, 30, 40, 7, 60] lst2 = [10, 34, 30, 89, 7, 60] # Set the condition based on the matching element matching_elements = [x for x, y in zip(lst1, lst2) if x == y] prod_idx = reduce(lambda x, y: x * y, matching_elements) if matching_elements else 1 # display the product print("Result of index match element Product:\n", prod_idx)
Output
Result of index match element Product: 126000
Conclusion
The index match elements are the common element that can be filtered by using some specific conditions and operations such as numpy library, functools library, list comprehension, and, for loop. There are some applications related to this program such as Data Analysis and Data Filteration.