Open In App

Python – Sort by Frequency of second element in Tuple List

Last Updated : 02 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Sort by Frequency of second element in Tuple List
# Using sorted() + loop + defaultdict() + lambda
from collections import defaultdict
 
# initializing list
test_list = [(6, 5), (2, 7), (2, 5), (8, 7), (9, 8), (3, 7)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# constructing mapping
freq_map = defaultdict(int)
for idx, val in test_list:
    freq_map[val] += 1
 
# performing sort of result
res = sorted(test_list, key = lambda ele: freq_map[ele[1]], reverse = True)
 
# printing results
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




# Python3 code to demonstrate working of
# Sort by Frequency of second element in Tuple List
# Using Counter() + lambda + sorted()
from collections import Counter
 
# initializing list
test_list = [(6, 5), (2, 7), (2, 5), (8, 7), (9, 8), (3, 7)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# constructing mapping using Counter
freq_map = Counter(val for key, val in test_list)
 
# performing sort of result
res = sorted(test_list, key = lambda ele: freq_map[ele[1]], reverse = True)
 
# printing results
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  # import groupby function from itertools module
 
def sort_by_frequency(test_list):  # define function called sort_by_frequency that takes a list called test_list as input
    freq_dict = {val: len(list(group)) for val, group in groupby(sorted(test_list, key=lambda x: x[1]), lambda x: x[1])} 
    # create a dictionary called freq_dict where each key is a unique second element of a tuple in test_list and its value is the number of times that second element appears in test_list
    # we do this by using the groupby function to group the tuples in test_list by their second element, then using len to count the number of tuples in each group
    # we use sorted to sort the list of tuples by their second element before using groupby, and we use a lambda function to specify that we want to group by the second element of each tuple
    # the resulting dictionary has keys that are unique second elements from test_list and values that are the frequency of each second element in test_list
    return sorted(test_list, key=lambda x: freq_dict[x[1]], reverse=True
    # sort the original list of tuples (test_list) based on the values in freq_dict
    # we use a lambda function to specify that we want to sort by the value in freq_dict corresponding to the second element of each tuple in test_list
    # we sort the list in reverse order (highest frequency first)
     
test_list = [(6, 5), (1, 7), (2, 5), (8, 7), (9, 8), (3, 7)]  # define test_list
print("The original list is : " + str(test_list))  # print the original list
print("The sorted list is : " + str(sort_by_frequency(test_list)))  # print the sorted list returned by the sort_by_frequency function


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
 
# initializing list
test_list = [(6, 5), (2, 7), (2, 5), (8, 7), (9, 8), (3, 7)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# convert the list to a numpy array
arr = np.array(test_list)
 
# get the frequency of each second element using numpy's unique function
counts = np.unique(arr[:, 1], return_counts=True)
 
# sort the indices based on the frequency of the second element using numpy's argsort function
sorted_indices = np.argsort(-counts[1])
 
# create an empty array to store the sorted tuples
sorted_arr = np.empty_like(arr)
 
# iterate over the sorted indices and fill in the sorted array
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
 
# convert the sorted array back to a list of tuples
res = [tuple(row) for row in sorted_arr]
 
# printing results
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 | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python - Find first element by second in tuple List
Sometimes, while working with Python records, we can have a problem in which we need to find the first element of tuple from the given second element. This kind of problem can occur in domains such as web development. Let's discuss certain ways in which this task can be performed. Input : test_list = [(4, 5), (5, 6), (1, 3), (6, 6)] K = 6 Output :
4 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Python | Largest, Smallest, Second Largest, Second Smallest in a List
Since, unlike other programming languages, Python does not have arrays, instead, it has list. Using lists is more easy and comfortable to work with in comparison to arrays. Moreover, the vast inbuilt functions of Python, make the task easier. So using these techniques, let's try to find the various ranges of the number in a given list. Examples: In
5 min read
Python Group by matching second tuple value in list of tuples
Given a list of tuples, the task is to group the tuples by matching the second element in the tuples. We can achieve this using dictionary by checking the second element in each tuple. Examples: Input : [(20, 80), (31, 80), (1, 22), (88, 11), (27, 11)] Output: {80: [(20, 80), (31, 80)], 11: [(88, 11), (27, 11)], 22: [(1, 22)]} Input : [(20, 'Geek')
3 min read
Python - Group first elements by second elements in Tuple list
Given List of tuples, group 1st elements on basis of 2nd elements. Input : test_list = [(6, 5), (2, 7), (2, 5), (8, 7), (3, 7)] Output : {5: [6, 2], 7: [2, 8, 3]} Explanation : 5 occurs along with 6 and 2 in tuple list, hence grouping. Input : test_list = [(6, 5), (2, 7), (2, 5), (8, 7)] Output : {5: [6, 2], 7: [2, 8]} Explanation : 5 occurs along
5 min read
Python | Sort a list according to the second element in sublist
In this article, we will learn how to sort any list, according to the second element of the sublist present within the main list. We will see two methods of doing this. We will learn three methods of performing this sort. One by the use of Bubble Sort, the second by using the sort() method, and last but not the least by the use of the sorted() meth
9 min read
Python - Flatten tuple of List to tuple
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform the flattening of tuples, which have listed as their constituent elements. This kind of problem is common in data domains such as Machine Learning. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ([5], [6], [3], [8]) O
7 min read
Python Program to Convert Tuple Matrix to Tuple List
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]] Output : [(4, 10), (5, 13)] Explanation : All co
8 min read
Python Program to find tuple indices from other tuple list
Given Tuples list and search list consisting of tuples to search, our task is to write a Python Program to extract indices of matching tuples. Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 2)]Output : [3, 1]Explanation : (3, 4) from search list is found on 3rd index on test_list, hence included in r
8 min read
Practice Tags :
three90RightbarBannerImg