Home » Python » Python programs

# Python program to sort tuples by frequency of their absolute difference

In this program, we have a list of tuples and we need to sort the tuples of the list based on the frequency of their absolute difference in Python programming language.

Submitted by Shivang Yadav, on July 16, 2021

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)

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered set of values enclosed in square brackets *[]*.

**Example:**

list = [3 ,1, 5, 7]

A list of tuples is a list whose each element is a tuple.

**Example:**

upList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]

## Sorting tuples by frequency of their absolute difference

We will be sorting all the tuples of the list based on the absolute difference of the elements of the tuples. We will put tuples with a single frequency of absolute difference 1 than 2 and so on.

Input: [(4,6), (1, 3), (6, 8), (4, 1), (5, 2)] Output: [(4, 1), (5, 2), (4, 6), (1, 3), (6, 8)]

We need to find the absolute difference of all elements of the tuple and count the frequency occurrence of the absolute difference and sort the list accordingly.

A way to sort the list of tuples using absolute difference is by first computing the absolute difference of each tuple using the ** abs()** method. Then sorting the tuples using the

*method and the*

**sorted()***method to sort the tuples based on the absolute difference count.*

**count()****Program:**

# Python program to sort tuples by frequency of # their absolute difference tupList = [(4,6), (1, 3), (6, 8), (4, 1), (5, 2)] print("Tuple list before sorting : " + str(tupList)) # Sorting Tuple list absList = [abs(x - y) for x, y in tupList] sortedTupList = sorted(tupList, key = lambda sub: absList.count(abs(sub[0] - sub[1]))) # print resulting list print("Tuple list before sorting : " + str(sortedTupList))

**Output:**

Tuple list before sorting : [(4, 6), (1, 3), (6, 8), (4, 1), (5, 2)] Tuple list before sorting : [(4, 1), (5, 2), (4, 6), (1, 3), (6, 8)]

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions