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
# Python3 code to demonstrate working of
# Sort Dictionaries by Size
# Using len() + sort()
# function to get length
def get_len(sub):
# return length
return len(sub)
# initializing list
test_list = [{4: 6, 9: 1, 10: 2, 2: 8}, {
4: 3, 9: 1}, {3: 9}, {1: 2, 9: 3, 7: 4}]
# printing original lists
print("The original list is : " + str(test_list))
# performing inplace sort of list
test_list.sort(key=get_len)
# printing result
print("Sorted List : " + str(test_list))
OutputThe 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
# Python3 code to demonstrate working of
# Sort Dictionaries by Size
# Using sorted() + len() + lambda
# initializing list
test_list = [{4: 6, 9: 1, 10: 2, 2: 8}, {
4: 3, 9: 1}, {3: 9}, {1: 2, 9: 3, 7: 4}]
# printing original lists
print("The original list is : " + str(test_list))
# performing sort using sorted(), lambda for filtering
res = sorted(test_list, key=lambda sub: len(sub))
# printing result
print("Sorted List : " + str(res))
OutputThe 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)
OutputOriginal 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,
2 min read