Python – Sort by Frequency of second element in Tuple List
Last Updated : 02 May, 2023
Given list of tuples, sort by frequency of second element of tuple.
Input : test_list = [(6, 5), (1, 7), (2, 5), (8, 7), (9, 8), (3, 7)]
Output : [(1, 7), (8, 7), (3, 7), (6, 5), (2, 5), (9, 8)]
Explanation : 7 occurs 3 times as 2nd element, hence all tuples with 7, are aligned first.
Input : test_list = [(1, 7), (8, 7), (9, 8), (3, 7)]
Output : [(1, 7), (8, 7), (3, 7), (9, 8)]
Explanation : 7 occurs 3 times as 2nd element, hence all tuples with 7, are aligned first.
Method #1 : Using sorted() + loop + defaultdict() + lambda
In this, we compute the frequency using defaultdict() and use this result to pass as param to lambda function to perform sorting using sorted() on basis of it.
Python3
from collections import defaultdict test_list = [( 6 , 5 ), ( 2 , 7 ), ( 2 , 5 ), ( 8 , 7 ), ( 9 , 8 ), ( 3 , 7 )] print ( "The original list is : " + str (test_list)) freq_map = defaultdict( int ) for idx, val in test_list: freq_map[val] + = 1 res = sorted (test_list, key = lambda ele: freq_map[ele[ 1 ]], reverse = True ) print ( "Sorted List of tuples : " + str (res)) |
Output The original list is : [(6, 5), (2, 7), (2, 5), (8, 7), (9, 8), (3, 7)] Sorted List of tuples : [(2, 7), (8, 7), (3, 7), (6, 5), (2, 5), (9, 8)]
Time Complexity: O(logn)
Auxiliary Space: O(n)
Method #2 : Using Counter() + lambda + sorted()
In this, the task of frequency computation is done using Counter(), rest all functionality is similar to above method.
Python3
from collections import Counter test_list = [( 6 , 5 ), ( 2 , 7 ), ( 2 , 5 ), ( 8 , 7 ), ( 9 , 8 ), ( 3 , 7 )] print ( "The original list is : " + str (test_list)) freq_map = Counter(val for key, val in test_list) res = sorted (test_list, key = lambda ele: freq_map[ele[ 1 ]], reverse = True ) print ( "Sorted List of tuples : " + str (res)) |
Output The original list is : [(6, 5), (2, 7), (2, 5), (8, 7), (9, 8), (3, 7)] Sorted List of tuples : [(2, 7), (8, 7), (3, 7), (6, 5), (2, 5), (9, 8)]
Time complexity: O(n log n), where n is the length of the input list test_list. The sorting operation takes O(n log n) time complexity, and constructing the frequency map using Counter() takes O(n) time complexity. Since O(n log n) is the dominant term.
Auxiliary Space: O(n), where n is the length of the input list test_list. This is because we are using a Counter() to construct a frequency map of the second element of each tuple in the input list, which takes O(n) auxiliary space. Additionally, we are storing the sorted list of tuples in memory, which also takes O(n) auxiliary space.
Method #3 : Using groupby() + sorted()
In this, the task of frequency computation is done by sorted() and groupby() functions from the itertools module.
Algorithm
Sort the input list of tuples by the second element. Count the frequency of each second element using a dictionary. Sort the input list of tuples by the frequency of the corresponding second element, in reverse order. Return the sorted list.
Python
from itertools import groupby def sort_by_frequency(test_list): freq_dict = {val: len ( list (group)) for val, group in groupby( sorted (test_list, key = lambda x: x[ 1 ]), lambda x: x[ 1 ])} return sorted (test_list, key = lambda x: freq_dict[x[ 1 ]], reverse = True ) test_list = [( 6 , 5 ), ( 1 , 7 ), ( 2 , 5 ), ( 8 , 7 ), ( 9 , 8 ), ( 3 , 7 )] print ( "The original list is : " + str (test_list)) print ( "The sorted list is : " + str (sort_by_frequency(test_list))) |
Output The original list is : [(6, 5), (1, 7), (2, 5), (8, 7), (9, 8), (3, 7)] The sorted list is : [(1, 7), (8, 7), (3, 7), (6, 5), (2, 5), (9, 8)]
Time complexity: O(n log n),where n is the length of test_list
Auxiliary Space: O(n),where n is the length of test_list
Method #4: Using numpy
- Convert the list of tuples into a numpy array.
- Use numpy’s argsort function to sort the array based on the frequency of the second element.
- Use numpy’s take function to get the sorted array based on the argsort indices.
- Convert the sorted array back to a list of tuples.
Python3
import numpy as np test_list = [( 6 , 5 ), ( 2 , 7 ), ( 2 , 5 ), ( 8 , 7 ), ( 9 , 8 ), ( 3 , 7 )] print ( "The original list is : " + str (test_list)) arr = np.array(test_list) counts = np.unique(arr[:, 1 ], return_counts = True ) sorted_indices = np.argsort( - counts[ 1 ]) sorted_arr = np.empty_like(arr) start = 0 for i in sorted_indices: freq = counts[ 1 ][i] indices = np.where(arr[:, 1 ] = = counts[ 0 ][i])[ 0 ] end = start + freq sorted_arr[start:end] = arr[indices] start = end res = [ tuple (row) for row in sorted_arr] print ( "Sorted List of tuples : " + str (res)) |
Output:
The original list is : [(6, 5), (2, 7), (2, 5), (8, 7), (9, 8), (3, 7)] Sorted List of tuples : [(2, 7), (8, 7), (3, 7), (6, 5), (2, 5), (9, 8)]
Time complexity: O(n log n) (due to sorting)
Auxiliary space: O(n) (due to creating a numpy array)
Similar Reads
Python - Step Frequency of elements in List
Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi
4 min read
Sort List Elements by Frequency - Python
Our task is to sort the list based on the frequency of each element. In this sorting process, elements that appear more frequently will be placed before those with lower frequency. For example, if we have: a = ["Aryan", "Harsh", "Aryan", "Kunal", "Harsh", "Aryan"] then the output should be: ['Aryan'
3 min read
Sort a List of Tuples by Second Item - Python
The task of sorting a list of tuples by the second item is common when working with structured data in Python. Tuples are used to store ordered collections and sometimes, we need to sort them based on a specific element, such as the second item. For example, given the list [(1, 3), (4, 1), (2, 2)],
2 min read
Python - Elements frequency in Tuple
Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
7 min read
Python - Elements frequency in Tuple Matrix
Sometimes, while working with Python Tuple Matrix, we can have a problem in which we need to get the frequency of each element in it. This kind of problem can occur in domains such as day-day programming and web development domains. Let's discuss certain ways in which this problem can be solved. Inp
5 min read
Python - Elements Frequency in Mixed Nested Tuple
Sometimes, while working with Python data, we can have a problem in which we have data in the form of nested and non-nested forms inside a single tuple, and we wish to count the element frequency in them. This kind of problem can come in domains such as web development and Data Science. Let's discus
8 min read
Python - List Frequency of Elements
We are given a list we need to count frequencies of all elements in given list. For example, n = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to count frequencies so that output should be {4: 4, 3: 3, 2: 2, 1: 1}. Using collections.Countercollections.Counter class provides a dictionary-like structure that
2 min read
Python - Sort Tuple List by Nth Element of Tuple
We are given list of tuple we need to sort tuple by Nth element of each tuple. For example d = [(1, 5), (3, 2), (2, 8), (4, 1)] and k=1 we need to sort by 1st element of each tuple so that output for given list should be [(4, 1), (3, 2), (1, 5), (2, 8)] Using sorted() with lambdasorted() function wi
3 min read
Python | Finding frequency in list of tuples
In python we need to handle various forms of data and one among them is list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the 1st element in list of tuple which can be extended to any index. Let's discuss cert
6 min read
Python - Restrict Tuples by frequency of first element's value
Given a Tuple list, the task is to write a Python program to restrict the frequency of the 1st element of tuple values to at most K. Examples: Input : test_list = [(2, 3), (3, 3), (1, 4), (2, 4), (2, 5), (3, 4), (1, 4), (3, 4), (4, 7)], K = 2 Output : [(2, 3), (3, 3), (1, 4), (2, 4), (3, 4), (1, 4),
3 min read