Sometimes, while working with Python list, one can have a problem in which one needs to find perform the product of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using loop This is the brute force method to perform this particular task. In this, we just iterate the list till last element in skipped manner to get all the pair products in other list in iterative way.
Python3
# Python3 code to demonstrate working of # List consecutive pair Product # Using loop # initializing list test_list = [ 5 , 8 , 3 , 5 , 9 , 10 ] # printing list print ( "The original list : " + str (test_list)) # List consecutive pair Product # Using loop res = [] for ele in range ( 0 , len (test_list), 2 ): res.append(test_list[ele] * test_list[ele + 1 ]) # Printing result print ( "Pair product of list : " + str (res)) |
The original list : [5, 8, 3, 5, 9, 10] Pair product of list : [40, 15, 90]
Time complexity: O(n) where n is the length of the input list
Auxiliary space: O(n/2), since we are creating a new list to store the results of the consecutive pair products.
Method #2 : Using zip() + list comprehension This task can also be performed using the combination of above functionalities. In this, we just iterate the list and the task of combining pairs is performed by zip(). Works only on Python2.
Python3
# Python code to demonstrate working of # List consecutive pair Product # Using zip() + list comprehension # initializing list test_list = [ 5 , 8 , 3 , 5 , 9 , 10 ] # printing list print ( "The original list : " + str (test_list)) # List consecutive pair Product # zip() + list comprehension res = [i * j for i, j in zip (test_list, test_list[ 1 :])[:: 2 ]] # Printing result print ( "Pair product of list : " + str (res)) |
The original list : [5, 8, 3, 5, 9, 10] Pair product of list : [40, 15, 90]
Time complexity: O(n), where n is the length of the input list ‘test_list’.
Auxiliary space: O(n),
Method #3 : Using islice()
In this approach, we used the islice method from itertools module, which allows us to slice an iterator by specifying the start, stop and step. Here, we use the zip function to combine every 2 elements of the list, then using islice method we slice the iterator to get every 2nd pair, and finally using list comprehension we find the product of the pairs. This approach is same as previous one but with added functionality of islice to only slice the required elements from iterator.
Python3
# Python code to demonstrate working of # List consecutive pair Product # Using zip() + list comprehension + islice from itertools import islice # initializing list test_list = [ 5 , 8 , 3 , 5 , 9 , 10 ] # printing list print ( "The original list : " + str (test_list)) # List consecutive pair Product # zip() + list comprehension + islice res = [i * j for i, j in islice( zip (test_list, test_list[ 1 :]), 0 , None , 2 )] # Printing result print ( "Pair product of list : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The original list : [5, 8, 3, 5, 9, 10] Pair product of list : [40, 15, 90]
Time complexity of this method is O(n) and
Auxiliary Space is O(n) where n is the number of elements in the list.
Method 4: Using a list comprehension with a conditional expression:
In this approach, we use a list comprehension to iterate through the indices of the list, multiplying each consecutive pair of elements together. We add a conditional expression to only include pairs whose index is even (i.e. the first element of the pair), since the original loop only multiplies pairs with an even index. We subtract 1 from the length of the list in the range() function so that we don’t go out of bounds when we access the last element of the list in the list comprehension.
Python3
test_list = [ 5 , 8 , 3 , 5 , 9 , 10 ] res = [test_list[i] * test_list[i + 1 ] for i in range ( len (test_list) - 1 ) if i % 2 = = 0 ] print ( "Pair product of list : " + str (res)) |
Pair product of list : [40, 15, 90]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), where n is the length of the input list.
Method #5: Using a generator expression with a conditional expression:
In this method, we use a generator expression to iterate over the list elements and multiply the consecutive pairs of elements that satisfy the condition (in this case, the indices must be even and less than the length of the list minus 1). The resulting generator expression is then converted to a list using the list() function.
Python3
# Python3 code to demonstrate working of # List consecutive pair Product # Using a generator expression # initializing list test_list = [ 5 , 8 , 3 , 5 , 9 , 10 ] # printing list print ( "The original list : " + str (test_list)) # List consecutive pair Product # Using a generator expression res = [test_list[ele] * test_list[ele + 1 ] for ele in range ( 0 , len (test_list) - 1 , 2 )] # Printing result print ( "Pair product of list : " + str (res)) |
The original list : [5, 8, 3, 5, 9, 10] Pair product of list : [40, 15, 90]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), because we are only using a constant amount of additional memory to store the input list and the output list.
Method #6: Using a reduce and lambda inbuilt function:
- Using zip function with slicing to create pairs of consecutive elements
- Using reduce function with a lambda function to multiply each pair of elements and store the results in a list.
- Printing the result.
Python3
from functools import reduce # initializing list test_list = [ 5 , 8 , 3 , 5 , 9 , 10 ] # printing list print ( "The original list : " + str (test_list)) # List consecutive pair Product # Using reduce and lambda res = reduce ( lambda ans, val: ans + [val[ 0 ] * val[ 1 ]], zip (test_list[:: 2 ], test_list[ 1 :: 2 ]), []) # Printing result print ( "Pair product of list : " + str (res)) |
The original list : [5, 8, 3, 5, 9, 10] Pair product of list : [40, 15, 90]
Time Complexity: O(N) where n is the length of the input list.
Auxiliary Space: O(N) as we are creating a new list
Method #7: Using NumPy
Step-by-step algorithm:
- Import the NumPy library as np.
- Create a list of numbers called test_list.
- Create a NumPy array by taking every other element in test_list (starting at the 0th index) and multiplying it by the corresponding element from the other half of the list.
- Convert the resulting NumPy array to a list using the tolist() method.
- Print out the resulting list.
Python3
import numpy as np # create a test list of numbers test_list = [ 5 , 8 , 3 , 5 , 9 , 10 ] # printing list print ( "The original list : " + str (test_list)) # create a numpy array by taking every other element in the test list (starting at the 0th index) # and multiplying it by the corresponding element from the other half of the list res = np.array(test_list[:: 2 ]) * np.array(test_list[ 1 :: 2 ]) # print out the resulting numpy array as a list print ( "Pair product of list : " + str (res.tolist())) |
Output:
The original list : [5, 8, 3, 5, 9, 10] Pair product of list : [40, 15, 90]
Time Complexity: O(N) where n is the length of the input list.
Auxiliary Space: O(N) as we are creating a new list