- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Find Maximum difference pair in Python

Data analysis can throw a variety of challenges. In this article we will take a list with numbers as its elements. Then we will find such pairs of elements in the list which has maximum difference in value between them.

## With nlargest

The approach here is to first find out all possible combinations of elements and then subtract the second element from the first. Finally apply the nlargest function form heapq module to get those pairs where the difference is maximum.

## Example

from itertools import combinations from heapq import nlargest listA = [21, 14, 30, 11, 17, 18] # Given list print("Given list : ",listA) # using nlargest and combinations() res = nlargest(2, combinations(listA, 2), key=lambda sub: abs(sub[0] - sub[1])) # print result print("Pairs with maximum difference are : ",res)

## Output

Running the above code gives us the following result −

Given list : [21, 14, 30, 11, 17, 18] Pairs with maximum difference are : [(30, 11), (14, 30)]

## With combinations and Max()

Here we also take the same approach as above but we get one pair as a result because we apply the max function which gives us one pair as the result.

## Example

from itertools import combinations listA = [21, 14, 30, 11, 17, 18] # Given list print("Given list : ",listA) # using combinations() and lambda res = max(combinations(listA, 2), key = lambda sub: abs(sub[0]-sub[1])) # print result print("Pairs with maximum difference are : ",res)

## Output

Running the above code gives us the following result −

Given list : [21, 14, 30, 11, 17, 18] Pairs with maximum difference are : (30, 11)

- Related Articles
- Find pair with maximum difference in any column of a Matrix in C++
- Program to find maximum distance between a pair of values in Python
- Find Maximum difference between tuple pairs in Python
- Find pair of rows in a binary matrix that has maximum bit difference in C++
- Find a pair from the given array with maximum nCr value in Python
- Count ways of choosing a pair with maximum difference in C++
- Find pair with maximum GCD in an array in C++
- Get minimum difference in Tuple pair in Python
- Swift Program to Find Maximum Key-Value Pair in the Dictionary
- Find maximum difference between nearest left and right smaller elements in Python
- Python - Maximum difference across lists
- Find a pair with maximum product in array of Integers in C++
- Queries to find maximum product pair in range with updates in C++
- Find a pair with the given difference in C++
- Find Sum of pair from two arrays with maximum sum in C++

Advertisements