Python – Sort Dictionaries by Size
Last Updated :
11 May, 2023
Given a Dictionary List, perform sort by size of dictionary.
Input : test_list = [{4:6, 9:1, 10:2, 2:8}, {4:3, 9:1}, {3:9}, {1:2, 9:3, 7:4}]
Output : [{3: 9}, {4: 3, 9: 1}, {1: 2, 9: 3, 7: 4}, {4: 6, 9: 1, 10: 2, 2: 8}]
Explanation : 1 < 2 < 3 < 4, sorted by number of keys of dictionary.
Input : test_list = [{4:3, 9:1}, {3:9}, {1:2, 9:3, 7:4}]
Output : [{3: 9}, {4: 3, 9: 1}, {1: 2, 9: 3, 7: 4}]
Explanation : 1 < 2 < 3, sorted by number of keys of dictionary.
Method #1 : Using len() + sort()
In this, sorting is performed using sort() and len() is used to get size of dictionary.
Python3
def get_len(sub):
return len (sub)
test_list = [{ 4 : 6 , 9 : 1 , 10 : 2 , 2 : 8 }, {
4 : 3 , 9 : 1 }, { 3 : 9 }, { 1 : 2 , 9 : 3 , 7 : 4 }]
print ( "The original list is : " + str (test_list))
test_list.sort(key = get_len)
print ( "Sorted List : " + str (test_list))
|
Output
The original list is : [{4: 6, 9: 1, 10: 2, 2: 8}, {4: 3, 9: 1}, {3: 9}, {1: 2, 9: 3, 7: 4}]
Sorted List : [{3: 9}, {4: 3, 9: 1}, {1: 2, 9: 3, 7: 4}, {4: 6, 9: 1, 10: 2, 2: 8}]
Time Complexity: O(n*nlogn) where n is the number of elements in the list “test_list”. len() + sort() performs n*nlogn number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #2 : Using sorted() + len() + lambda
Here, we perform task of sorting using sorted(), and lambda function is used in place of external function to solve problem of getting length.
Python3
test_list = [{ 4 : 6 , 9 : 1 , 10 : 2 , 2 : 8 }, {
4 : 3 , 9 : 1 }, { 3 : 9 }, { 1 : 2 , 9 : 3 , 7 : 4 }]
print ( "The original list is : " + str (test_list))
res = sorted (test_list, key = lambda sub: len (sub))
print ( "Sorted List : " + str (res))
|
Output
The original list is : [{4: 6, 9: 1, 10: 2, 2: 8}, {4: 3, 9: 1}, {3: 9}, {1: 2, 9: 3, 7: 4}]
Sorted List : [{3: 9}, {4: 3, 9: 1}, {1: 2, 9: 3, 7: 4}, {4: 6, 9: 1, 10: 2, 2: 8}]
Time Complexity: O(n*nlogn), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
METHOD 3:Using map method.
APPROACH:
The given problem statement asks to sort a list of dictionaries based on the size of each dictionary.
ALGORITHM:
1. Define a function to get the length of the dictionary.
2. Use map() to apply the function to each dictionary in the original list.
3. Use zip() to combine the list of lengths with the original list of dictionaries.
4. Use sorted() to sort the list of tuples based on the length of each dictionary.
5. Extract the sorted list of dictionaries using a list comprehension.
Python3
original_list = [{ 4 : 6 , 9 : 1 , 10 : 2 , 2 : 8 }, { 4 : 3 , 9 : 1 }, { 3 : 9 }, { 1 : 2 , 9 : 3 , 7 : 4 }]
def get_dict_length(d):
return len (d)
sorted_list = [x for _, x in sorted ( zip ( map (get_dict_length, original_list), original_list))]
print ( "Original List:" , original_list)
print ( "Sorted List:" , sorted_list)
|
Output
Original List: [{4: 6, 9: 1, 10: 2, 2: 8}, {4: 3, 9: 1}, {3: 9}, {1: 2, 9: 3, 7: 4}]
Sorted List: [{3: 9}, {4: 3, 9: 1}, {1: 2, 9: 3, 7: 4}, {4: 6, 9: 1, 10: 2, 2: 8}]
Time Complexity:
The time complexity of this solution is O(n log n), where n is the number of dictionaries in the list. This is due to the sorting operation, which has a time complexity of O(n log n).
Space Complexity:
The space complexity of this solution is O(n), where n is the number of dictionaries in the list. This is due to the creation of a new list containing the lengths of each dictionary in the original list.
Similar Reads
Sort a Dictionary - Python
In Python, dictionaries store key-value pairs and are great for organizing data. While they werenât ordered before Python 3.7, you can still sort them easily by keys or values, in ascending or descending order. Whether youâre arranging names alphabetically or sorting scores from highest to lowest, P
5 min read
Sort Python Dictionary by Value
Python dictionaries are versatile data structures that allow you to store key-value pairs. While dictionaries maintain the order of insertion. sorting them by values can be useful in various scenarios. In this article, we'll explore five different methods to sort a Python dictionary by its values, a
3 min read
Python | Sort nested dictionary by key
Sorting has quite vivid applications and sometimes, we might come up with a problem in which we need to sort the nested dictionary by the nested key. This type of application is popular in web development as JSON format is quite popular. Let's discuss certain ways in which this can be performed. Met
4 min read
Python - Sort List by Dictionary values
Sometimes while working with a Python dictionary, we can have problems in which we need to perform a sort of list according to the corresponding value in the dictionary. This can have applications in many domains, including data and web development. Let's discuss certain ways in which this task can
3 min read
Get Size of a Dictionary - Python
We are given a dictionary in Python and our task is to find the memory size that the dictionary occupies. This can be important when you need to know how much memory your data structures are consuming especially for large dictionaries. For example, if we have a dictionary like this: {'a': 1, 'b': 2,
3 min read
Sort a Nested Dictionary by Value in Python
Sorting a nested dictionary in Python involves understanding its structure, defining sorting criteria, and utilizing the `sorted()` function or `.sort()` method with a custom sorting function, often a lambda. This process is essential for organizing complex, hierarchical data efficiently. Mastery of
3 min read
Python - Rotate dictionary by K
Given a dictionary perform its reordering by right rotating dictionary keys by K. Examples: Input : test_dict = {1:6, 8:1, 9:3, 10:8, 12:6, 4:9}, K = 2 Output : {12: 6, 4: 9, 1: 6, 8: 1, 9: 3, 10: 8} Explanation : After right rotation ( cyclic ) by 2 elements, items are re-arranged. Input : test_dic
4 min read
Python - Sort Dictionary by Values and Keys
Given a dictionary, sort according to descended values, if similar values, then by keys lexicographically. Input : test_dict = {"gfg" : 1, "is" : 1, "best" : 1, "for" : 1, "geeks" : 1} Output : {"best" : 1, "is" : 1, "for" : 1, "geeks" : 1, "gfg" : 1} Explanation : All values are equal, hence lexico
3 min read
Sort a List of Python Dictionaries by a Value
Sorting a list of dictionaries by a specific value is a common task in Python programming. Whether you're dealing with data manipulation, analysis, or simply organizing information, having the ability to sort dictionaries based on a particular key is essential. In this article, we will explore diffe
3 min read
Python - Sort Dictionary by Value Difference
Sometimes, while working with Python dictionaries, we can have problem in which in which we need to perform sorting of items on basis of various factors. One such can be on basis of absolute difference of dual value list. This can occur in Python > 3.6, as dictionaries are ordered. This kind of p
3 min read