Sometimes, we need to find the specific problem of getting the pair which yields the maximum difference, this can be solved by sorting and getting the first and last elements of the list. But in some case, we don’t with to change the ordering of list and perform some operation in a similar list without using extra space. Let’s discuss certain ways in which this can be performed.
Method #1 : Using list comprehension + max() + combination() + lambda
This particular task can be performed using the combination of above functions in which we use list comprehension to bind all the functionalities and max function to get the maximum difference, combination function finds all differences internally and lambda function is used to compute the difference.
# Python3 code to demonstrate # maximum difference pair # using list comprehension + max() + combinations() + lambda from itertools import combinations # initializing list test_list = [3, 4, 1, 7, 9, 1] # printing original list print("The original list : " + str(test_list)) # using list comprehension + max() + combinations() + lambda # maximum difference pair res = max(combinations(test_list, 2), key = lambda sub: abs(sub[0]-sub[1])) # print result print("The maximum difference pair is : " + str(res)) |
The original list : [3, 4, 1, 7, 9, 1] The maximum difference pair is : (1, 9)
Method #2 : Using list comprehension + nlargest() + combination() + lambda
This method has potential of not only finding a single maximum but also k maximum difference pairs if required and uses nlargest function instead of max function to achieve this functionality.
# Python3 code to demonstrate # maximum difference pair # using list comprehension + nlargest() + combinations() + lambda from itertools import combinations from heapq import nlargest # initializing list test_list = [3, 4, 1, 7, 9, 8] # printing original list print("The original list : " + str(test_list)) # using list comprehension + max() + combinations() + lambda # maximum difference pair # computes 2 maximum pair differences res = nlargest(2, combinations(test_list, 2), key = lambda sub: abs(sub[0]-sub[1])) # print result print("The maximum difference pair is : " + str(res)) |
The original list : [3, 4, 1, 7, 9, 8] The maximum difference pair is : [(1, 9), (1, 8)]
Recommended Posts:
- Python - Maximum Pair Summation in numeric String
- Python - Pair with Maximum product
- Python - Maximum of Consecutive Pair in integer list
- Python - Maximum Quotient Pair in List
- Python | Maximum modulo pair
- Python - Maximum Aggregation Pair in List
- Python - Get minimum difference in Tuple pair
- Python List Comprehension to find pair with given sum from two arrays
- Python Program for Find the closest pair from two sorted arrays
- Python - Find Minimum Pair Sum in list
- Python | Find Maximum difference between tuple pairs
- Python | Pair iteration in list
- Python | Pair and combine nested list to tuple list
- Python | Count the Number of matching characters in a pair of string
- Python | Make pair from two list such that elements are not same in pairs
- Add a key:value pair to dictionary in Python
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert key-value pair comma separated string into dictionary
- Python | Ways to join pair of elements in list
- Python | Insert character after every character pair
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

