Python – Product of consecutive pairs in list
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]
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]
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]
This approach is more efficient as it is using itertools library which is written in C, thus faster than python implementation.
Time complexity of this method is O(n) and Auxiliary Space is O(n) where n is the number of elements in the list.
Please Login to comment...