Given List of tuples, find index-wise absolute difference of consecutive tuples.
Input : test_list = [(5, 3), (1, 4), (9, 5), (3, 5)] Output : [(4, 1), (7, 1), (6, 0)] Explanation : 5 – 1 = 4, 4 – 3 = 1. hence (4, 1) and so on. Input : test_list = [(9, 5), (3, 5)] Output : [(6, 0)] Explanation : 9 – 3 = 6, 5 – 5 = 0.
Method #1 : Using list comprehension
This is one way in which this task can be performed. In this, we employ brute force strategy on list using list comprehension strategy to perform this task as a one-liner.
Python3
# Python3 code to demonstrate working of # Consecutive Tuple difference # Using list comprehension # initializing lists test_list = [( 6 , 3 ), ( 1 , 4 ), ( 8 , 5 ), ( 3 , 5 )] # printing original list print ( "The original list : " + str (test_list)) # using list comprehension to perform absolute difference # using abs() for difference res = [( abs (test_list[idx + 1 ][ 0 ] - test_list[idx][ 0 ]), abs (test_list[idx + 1 ][ 1 ] - test_list[idx][ 1 ])) for idx in range ( len (test_list) - 1 )] # printing result print ( "The extracted list : " + str (res)) |
The original list : [(6, 3), (1, 4), (8, 5), (3, 5)] The extracted list : [(5, 1), (7, 1), (5, 0)]
Time Complexity: O(n), where n is the elements in the list
Auxiliary Space: O(n), where n is the size of list
Method #2 : Using tuple() + map() + sub
The combination of above functions can be used to solve this problem. In this, we perform the task of perform subtraction using sub and map() is used to extend the logic to whole list elements. Doesn’t output absolute result.
Python3
# Python3 code to demonstrate working of # Consecutive Tuple difference # Using tuple() + map() + sub from operator import sub # initializing lists test_list = [( 6 , 3 ), ( 1 , 4 ), ( 8 , 5 ), ( 3 , 5 )] # printing original list print ( "The original list : " + str (test_list)) # using loop to iterate elements # using sub and map() to perform the subtraction to whole elements res = [] for idx in range ( len (test_list) - 1 ): res.append( tuple ( map (sub, test_list[idx + 1 ][ 0 :], test_list[idx][ 0 :]))) # printing result print ( "The extracted list : " + str (res)) |
The original list : [(6, 3), (1, 4), (8, 5), (3, 5)] The extracted list : [(-5, 1), (7, 1), (-5, 0)]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method 3 : Using numpy library
Step by step approach :
- The numpy module is imported with the alias “np”.
- A list named “test_list” is initialized with four tuples as its elements.
- The original list is printed using the “print()” function with a string concatenation of “The original list : ” and the list converted to a string.
- The list “test_list” is converted to a numpy array named “arr” using the “np.array()” function.
- A new numpy array named “diff_arr” is created by subtracting the first n-1 elements of “arr” from the last n-1 elements of “arr”, where n is the length of the array.
- The numpy array “diff_arr” is converted back to a list of tuples named “res” using a list comprehension and the “tuple()” function.
- The extracted list is printed using the “print()” function with a string concatenation of “The extracted list : ” and the list converted to a string.
Python3
import numpy as np # initializing lists test_list = [( 6 , 3 ), ( 1 , 4 ), ( 8 , 5 ), ( 3 , 5 )] # printing original list print ( "The original list : " + str (test_list)) # converting the list to a numpy array arr = np.array(test_list) # using slicing to extract consecutive tuples diff_arr = arr[ 1 :] - arr[: - 1 ] # converting the numpy array back to a list of tuples res = [ tuple (row) for row in diff_arr] # printing the result print ( "The extracted list : " + str (res)) |
OUTPUT : The original list : [(6, 3), (1, 4), (8, 5), (3, 5)] The extracted list : [(-5, 1), (7, 1), (-5, 0)]
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 (used to store the numpy array).