Python | Find Maximum difference between tuple pairs
Sometimes, while working with data, we might have a problem in which we need to find maximum difference between available pairs in list. This can be application to many problems in mathematics domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using max() + list comprehension
The combination of this functions can be used to perform this task. In this, we compute the absolute difference of all pairs and then return the max of it using max().
Python3
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
print ( "The original list : " + str (test_list))
temp = [ abs (b - a) for a, b in test_list]
res = max (temp)
print ( "Maximum difference among pairs : " + str (res))
|
Output
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum difference among pairs : 7
Method #2 : Using max() + lambda
This is similar to above method. In this the task performed by list comprehension is solved using lambda function, providing the difference computation logic. Returns the max. difference pair.
Python3
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
print ( "The original list : " + str (test_list))
res = max (test_list, key = lambda sub: abs (sub[ 1 ] - sub[ 0 ]))
print ( "Maximum difference among pairs : " + str (res))
|
Output
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum difference among pairs : (10, 3)
Method #3: Using reduce() function and a lambda function
This approach uses the reduce() function to iterate through the elements of the list and a lambda function to compute the maximum difference between the elements of each tuple.
Python3
from functools import reduce
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
print ( "The original list : " + str (test_list))
max_pair = reduce ( lambda x, y: x if abs (x[ 1 ] - x[ 0 ]) > abs (y[ 1 ] - y[ 0 ]) else y, test_list)
print ( "Maximum difference among pairs : " + str (max_pair))
|
Output
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum difference among pairs : (10, 3)
The time complexity of this method is also O(n) and Auxiliary space is O(1) as well.
Method 4 : using a for loop:
This code iterates over each pair in the list and compares the absolute difference of each pair to the maximum difference seen so far. If the absolute difference of the current pair is greater than the maximum difference seen so far, then the current pair becomes the new max_pair.
step by step approach of the code you provided:
- Initialize a list of tuples test_list with four tuples containing two elements each.
- Print the original list using the print() function and str() to convert the list to a string.
- Initialize two variables max_pair and max_diff. max_pair will eventually hold the tuple pair with the maximum absolute difference, and max_diff will hold the value of the maximum absolute difference seen so far.
- Loop through each tuple pair in the list using a for loop.
- Calculate the absolute difference between the two elements in the current pair using the abs() function.
- Check if the current difference is greater than the current maximum difference (max_diff). If it is, update the values of max_pair and max_diff.
- After the loop finishes, print the tuple pair with the maximum absolute difference using the print() function and str() to convert the tuple to a string.
Python3
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
print ( "The original list : " + str (test_list))
max_pair = None
max_diff = float ( '-inf' )
for pair in test_list:
diff = abs (pair[ 1 ] - pair[ 0 ])
if diff > max_diff:
max_pair = pair
max_diff = diff
print ( "Maximum difference among pairs : " + str (max_pair))
|
Output
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum difference among pairs : (10, 3)
The time complexity of this approach is O(n) since it iterates over the list once.
The space complexity is O(1) since it only stores a constant number of variables (max_pair and max_diff).
Method #5: Using map() and abs() with key argument
- First, we use map() and a lambda function to create a list of absolute differences between the pairs in test_list.
- Next, we use the index() method to find the index of the maximum absolute difference in the abs_diffs list.
- Finally, we use the max_index to find the corresponding tuple pair with the maximum difference in test_list.
Python3
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
abs_diffs = list ( map ( lambda x: abs (x[ 0 ] - x[ 1 ]), test_list))
max_index = abs_diffs.index( max (abs_diffs))
max_pair = test_list[max_index]
print ( "Maximum difference among pairs : " + str (max_pair))
|
Output
Maximum difference among pairs : (10, 3)
Time complexity: O(n), where n is the length of test_list
Auxiliary space: O(n), where n is the length of test_list
Last Updated :
07 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...