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 multiplication. Lets discuss certain ways to achieve this task.
Method #1 : Using List comprehension + loop
This task is easy to perform with a loop, and hence shorthand for it is the first method to start with this task. Iterating over the index list to get the corresponding elements from list into new list is brute method to perform this task. The task of product is performed using loop.
Python3
# Python3 code to demonstrate # Product of Index values # using list comprehension + loop # Getting Product def prod(val) : res = 1 for ele in val: res * = ele return res # Initializing lists test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ] index_list = [ 1 , 3 , 4 ] # Printing original lists print ( "Original list : " + str (test_list)) print ( "Original index list : " + str (index_list)) # Product of Index values # using list comprehension + loop res_list = prod([test_list[i] for i in index_list]) # Printing result print ( "Resultant list : " + str (res_list)) |
Original list : [9, 4, 5, 8, 10, 14] Original index list : [1, 3, 4] Resultant list : 320
Time Complexity: O(n) where n is the length of the index_list.
Auxiliary Space: O(1) as only a few variables are used and no additional data structure is used.
Method #2 : Using map() + __getitem__ + loop
Yet another method to achieve this particular task is to map one list with other and get items of indexes and get corresponding matched elements from the search list. This is quite quick way to perform this task. The task of product is performed using loop.
Python3
# Python3 code to demonstrate # Product of Index values # using map() + __getitem__ + loop # getting Product def prod(val): res = 1 for ele in val: res * = ele return res # Initializing lists test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ] index_list = [ 1 , 3 , 4 ] # Printing original lists print ( "Original list : " + str (test_list)) print ( "Original index list : " + str (index_list)) # Product of Index values # using map() + __getitem__ + loop to res_list = prod( list ( map (test_list.__getitem__, index_list))) # Printing result print ( "Resultant list : " + str (res_list)) |
Original list : [9, 4, 5, 8, 10, 14] Original index list : [1, 3, 4] Resultant list : 320
Time Complexity: O(n), where n is the number of elements in the index_list.
Auxiliary Space: O(n), where n is the number of elements in the index_list.
Method #3 : Using reduce() of functools and operator
Python3
# Python3 code to demonstrate # Product of Index values # initializing lists test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ] index_list = [ 1 , 3 , 4 ] # printing original lists print ( "Original list : " + str (test_list)) print ( "Original index list : " + str (index_list)) x = [] for i in index_list: x.append(test_list[i]) from functools import reduce import operator res = reduce (operator.mul,x, 1 ) # printing result print ( "Resultant list : " + str (res)) |
Original list : [9, 4, 5, 8, 10, 14] Original index list : [1, 3, 4] Resultant list : 320
Time complexity: O(n), where n is the number of elements in the index list.
Auxiliary space: O(m), where m is the number of elements in the index list.
Method 5: Using numpy
Python3
#Importing NumPy import numpy as np #Initializing lists test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ] index_list = [ 1 , 3 , 4 ] #Get product using NumPy result = np.prod(np.array(test_list)[index_list]) #Printing result print ( "Resultant list :" , result) |
Output:
Resultant list : 320
Time complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using a for loop and the math library
Approach:
- Import the math library using import math.
- Initialize a variable named product to 1.
- Loop through each index in the index_list using a for loop.
- Access the corresponding element in test_list using the current index and multiply it with the product variable.
- After the loop, print the product variable to get the result.
Below is the implementation of the above approach:
Python3
import math #Initializing lists test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ] index_list = [ 1 , 3 , 4 ] #Calculating the product using a for loop and the math library product = 1 for i in index_list: product * = test_list[i] #Printing result print ( "Resultant list :" , product) |
Resultant list : 320
Time complexity: O(n), where n is the length of index_list.
Auxiliary space: O(1), as we only use a constant amount of extra memory to store the product variable.
Method #6: Using a lambda function and reduce()
Approach:
- Import the reduce() function from functools.
- Initialize the test_list and index_list as given in the problem statement.
- Define a lambda function that takes two arguments and returns their product.
- Use the reduce() function with the lambda function and a list comprehension to calculate the product of the elements of test_list at the indices given in index_list.
- Assign the product to a variable and print it.
Python3
from functools import reduce # Initializing lists test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ] index_list = [ 1 , 3 , 4 ] # Defining a lambda function for product prod_lambda = lambda x, y: x * y # Calculating the product using reduce() and a list comprehension product = reduce (prod_lambda, [test_list[i] for i in index_list]) # Printing result print ( "Resultant list :" , product) |
Resultant list : 320
Time complexity: O(n) where n is the length of the index_list.
Auxiliary space: O(m) where m is the length of the index_list, as we are storing a list of m elements extracted from the test_list.
Method #7: Using the itertools library
- Import necessary modules:
- reduce function from functools module to perform a specific operation (in this case, multiplication) on all elements of a list.
itemgetter function from operator module to extract elements from a list using indices.
combinations function from itertools module to generate all possible combinations of indices.
Initialize the test_list and index_list variables: - test_list is a list of integers.
index_list is a list of integers representing the indices of elements to be extracted from test_list.
Define a lambda function prod_lambda that takes a sublist and returns the product of all its elements using the reduce function. - Generate all possible combinations of indices using the combinations function and store the result in the index_combinations variable.
- Extract corresponding elements of test_list for each combination of indices using the itemgetter function and store the result in the sub_lists variable. For each combination, the itemgetter function is called with the indices as argument to extract the corresponding elements from test_list, and the resulting sublist is passed to prod_lambda to compute its product.
- Take the maximum of the resulting sublists using the max function and store the result in the product variable.
- Print the maximum product obtained from all the sublists using the print function.
Python3
from functools import reduce from operator import itemgetter from itertools import combinations # Initializing lists test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ] index_list = [ 1 , 3 , 4 ] # Define lambda function for product def prod_lambda(sub_list): return reduce ( lambda x, y: x * y, sub_list) # Generate all possible combinations of indices index_combinations = combinations(index_list, len (index_list)) # Extract corresponding elements of test_list for each combination of indices sub_lists = [prod_lambda(itemgetter( * indices)(test_list)) for indices in index_combinations] # Take the maximum of the resulting sublists product = max (sub_lists) # Printing result print ( "Resultant list :" , product) |
Resultant list : 320
Time complexity: O(n^2), where n is the length of index_list.
Auxiliary space: O(1)