Currently, I am trying to learn more about how to utilize comprehensions. However, I am stuck with this problem I have right now.
The dictionary I have:
d = {'Second': {('Carrie', 3), ('Diane', 3), ('Bob', 3)},
'Third': {('Alan', 2), ('Diane', 5)},
'Fourth': {('Carrie', 2), ('Alan', 5)},
'First': {('Bob', 5), ('Carrie', 5), ('Alan', 1), ('Diane', 1)} }
I am trying to sort it by the number of values each key has so it would look like this when returned. If the key both have the same amount of values, it would be sorted alphabetically.
Desired Output:
['First', 'Second', 'Fourth', 'Third']
What I have so far however, I am unsure how to sort by the len of the values :
d = {'Second': {('Carrie', 3), ('Diane', 3), ('Bob', 3)}, 'Third': {('Alan', 2), ('Diane', 5)}, 'Fourth': {('Carrie', 2), ('Alan', 5)}, 'First': {('Bob', 5), ('Carrie', 5), ('Alan', 1), ('Diane', 1)} }
def sorting(d):
return sorted([key for key, value in db.items(), key = lambda x: -len( )