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.
Share your thoughts in the comments
Please Login to comment...