Open In App

Python – Sort Dictionaries by Size

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

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))


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




# 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))


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads