Open In App

Python – Consecutive Kth column Difference in Tuple List

Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the absolute difference of it’s Kth index. This problem has application in web development domain while working with data informations. Let’s discuss certain ways in which this task can be performed.

Examples:



Input : test_list = [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)], K = 0 
Output : [4, 4, 2] 
Explanation : 5 – 1 = 4, hence 4. 

Input : test_list = [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)], K = 2 
Output : [2, 4, 5] 
Explanation : 8 – 3 = 5, hence 5. 



Method #1: Using loop

In this, for each tuple we subtract and find absolute difference of Kth column tuples with consecutive tuples in list.

Step-by-step approach :

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Consecutive Kth column Difference in Tuple List
# Using loop
 
# initializing list
test_list = [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
res = []
for idx in range(0, len(test_list) - 1):
 
    # getting difference using abs()
    res.append(abs(test_list[idx][K] - test_list[idx + 1][K]))
     
# printing result
print("Resultant tuple list : " + str(res))

Output:

The original list is : [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)] Resultant tuple list : [1, 4, 3]

Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(n),

Method #2: Using zip() + list comprehension

In this, we iterate for all the element in list using list comprehension and compare elements paired using zip(). 




# Python3 code to demonstrate working of
# Consecutive Kth column Difference in Tuple List
# Using zip() + list comprehension
 
# initializing list
test_list = [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
# zip used to pair each tuple with subsequent tuple
res = [abs(x[K] - y[K]) for x, y in zip(test_list, test_list[1:])]
     
# printing result
print("Resultant tuple list : " + str(res))

Output:

The original list is : [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)] Resultant tuple list : [1, 4, 3]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as it creates a new list of size n to store the result.

Method #3: Using numpy library .




import numpy as np
 
# initializing list
test_list = [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
# convert the tuple list to a numpy array
arr = np.array(test_list)
 
# calculate the consecutive Kth column difference using numpy
res = np.abs(np.diff(arr[:, K]))
 
# printing result
print("Resultant tuple list : " + str(list(res)))

OUTPUT:
The original list is : [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)]
Resultant tuple list : [1, 4, 3]

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), which is the space required to store the numpy array created from the input list.

Method #4 : Using list slicing




# Python3 code to demonstrate working of
# Consecutive Kth column Difference in Tuple List
# Using list slicing
 
# initializing list
test_list = [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 1
 
# using list slicing to get Kth column difference
res = [abs(test_list[i][K] - test_list[i+1][K]) for i in range(len(test_list)-K)]
 
# printing result
print("Resultant tuple list : " + str(res))

Output
The original list is : [(5, 4, 2), (1, 3, 4), (5, 7, 8), (7, 4, 3)]
Resultant tuple list : [1, 4, 3]

Time complexity: O(n), where n is the length of the “test_list”.
Auxiliary space: O(n), where n is the length of the “test_list”.


Article Tags :