Open In App
Related Articles

Python – Get minimum difference in Tuple pair

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Sometimes, while working with data, we might have a problem in which we need to find minimum 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 min() + 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 min of it using min().

Python3

# Python3 code to demonstrate working of
# Tuple minimum difference in pair
# Using list comprehension + min()
 
# initialize list
test_list = [(3, 5), (1, 7), (10, 3), (1, 2)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Tuple minimum difference in pair
# Using list comprehension + min()
temp = [abs(b - a) for a, b in test_list]
res = min(temp)
 
# printing result
print("Minimum difference among pairs : " + str(res))

                    

Output : 
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Minimum difference among pairs : 1

 

 
Method #2 : Using min() + 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 min. difference pair.

Python3

# Python3 code to demonstrate working of
# Tuple minimum difference in pair
# Using lambda + min()
 
# initialize list
test_list = [(3, 5), (1, 7), (10, 3), (1, 2)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Tuple minimum difference in pair
# Using lambda + min()
res = min(test_list, key = lambda sub: abs(sub[1] - sub[0]))
 
# printing result
print("Minimum difference among pairs : " + str(res))

                    

Output : 
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Minimum difference among pairs : (1, 2)

 

Method #3 : Using reduce() 

Python3

# Python3 code to demonstrate working of
# Tuple minimum difference in pair
# Using reduce() + lambda
  
# importing reduce from functools
from functools import reduce
  
# initialize list
test_list = [(3, 5), (1, 7), (10, 3), (1, 2)]
  
# printing original list
print("The original list : " + str(test_list))
  
# Tuple minimum difference in pair
# Using reduce() + lambda
res = reduce((lambda x, y : x if abs(x[1] - x[0]) < abs(y[1] - y[0]) else y), test_list)
  
# printing result
print("Minimum difference among pairs : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy

                    

Output
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Minimum difference among pairs : (1, 2)

Time Complexity – O(n) 
Auxiliary Space – O(1)

Method #4: Using a for loop

  1. The list test_list is initialized with four tuples, each containing two integers.
  2. The original list is printed using the print() function with a string message and the str() function to convert the list object to a string.
  3. Two variables min_pair and min_diff are initialized to the first tuple in the list and the absolute difference between its two elements, respectively.
  4. A for loop is used to iterate over the remaining tuples in the list.
  5. For each tuple, the absolute difference between its two elements is calculated and compared to the current minimum difference. If it is smaller, min_pair and min_diff are updated to the current tuple and its difference, respectively.
  6. The tuple with the minimum difference is stored in the variable min_pair.
  7. The result is printed using the print() function with a string message and the str() function to convert the tuple object to a string.

Python3

# Python3 code to demonstrate working of
# Tuple minimum difference in pair
# Using for loop
 
# initialize list
test_list = [(3, 5), (1, 7), (10, 3), (1, 2)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Tuple minimum difference in pair
# Using for loop
min_pair = test_list[0]
min_diff = abs(min_pair[1] - min_pair[0])
for pair in test_list[1:]:
    diff = abs(pair[1] - pair[0])
    if diff < min_diff:
        min_pair = pair
        min_diff = diff
 
# printing result
print("Minimum difference among pairs : " + str(min_pair))

                    

Output
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Minimum difference among pairs : (1, 2)

The time complexity of this method is O(n), where n is the length of the list.

 The auxiliary space complexity is O(1), since only a few variables are used to store the minimum pair and its difference.



Last Updated : 23 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads