Python program to find the maximum difference between tuple pairs

In this problem, we are given a list of tuples consisting of integer values. Our task is to create a Python program to find the maximum difference between tuple pairs.
Submitted by Shivang Yadav, on January 16, 2022

Extracting collections based on some given condition can be beneficial while working with complex structures. Python provides multiple methods to perform such tasks. Here, we will see a Python program to find the maximum difference between tuple pairs.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Finding the maximum difference between tuple pairs

We are given a list of tuples with integer values. We need to create a Python program to find the maximum difference between tuple pairs.

Input: 
tupList = [(5, 7), (2, 6), (1, 9), (1, 3)]

Output:
8

Explanation: 

Absolute difference of all tuples : 
(5, 7) = 2
(2, 6) = 4
(1, 9) = 8
(1, 3) = 2

To perform this task, we will iterate over the list and find the difference of elements of each tuple. And return the maximum of all of them. In Python, we have some methods and ways that can ease the task for us.

Method 1:

One method to solve the problem is by creating a list with the absolute difference of all tuples of the list. And find the maximum value out of all these values.

# Python program to find the maximum difference 
# between tuple pairs

# Initializing and printing list
tupList = [(5, 7), (2, 6), (1, 9), (1, 3)]
print("The elements of list of tuples is " + str(tupList))

# Maximum difference between tuple pairs
maxTupDiff =max([abs(val2 - val1) for val1, val2 in tupList])

# Printing result
print("The Maximum absolute difference is " + str(maxTupDiff))

Output:

The elements of list of tuples is [(5, 7), (2, 6), (1, 9), (1, 3)]
The Maximum absolute difference is 8

Method 2:

Another approach is by using a lambda function to find the absolute difference of tuples of the list. The resultant is then passed to the max() function. And calculate the absolute difference.

# Initializing and printing list
tupList = [(5, 7), (2, 6), (1, 9), (1, 3)]
print("The elements of list of tuples is " + str(tupList))

# Maximum difference between tuple pairs
maxDiffTup = max(tupList, key = lambda tup: abs(tup[1] - tup[0]))

# Printing result
print("The Maximum absolute difference is " + str(maxDiffTup[1] - maxDiffTup[0]))

Output:

The elements of list of tuples is [(5, 7), (2, 6), (1, 9), (1, 3)]
The Maximum absolute difference is 8

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.