I have a dictionary as follows.
my_d={"0":["code_5", "code_4", "code_10"],
"1":["code_1", "code_2", "code_3", "code_11"],
"2": ["code_7", "code_8"], "3": ["code_9"]}
I want to sort the dictionary by considering the number of elements in its list. That is "1": ["code_1", "code_2", "code_3"] has 3 elements, so should be in the first place of the dictionary. Hence my output should be as follows.
my_d = {"1":["code_1", "code_2", "code_3", "code_11"],
"0":["code_5", "code_4", "code_10"],
"2": ["code_7", "code_8"], "3": ["code_9"]}
Now I want to get only the first 2 keys of the dictionary. So, my final output should look as follows.
my_d={"1": ["code_1", "code_2", "code_3", "code_11"],
"0":["code_5", "code_4", "code_10"]}
My files are very large. So I want a quick and efficient way of doing this in python. Please help me!
sorted(mydict.iterkeys())sorted(d.values(), key=lambda value: -len(value))