Python - Sorting a dictionary {String Key: List of Strings} by len(value)? -


suppose have following dictionary:

{"a":["u","w"],"b":["t"],"c":["x","y","z"]} 

how can sort dictionary number of strings in list in of each value returns:

[("c",["x","y","z"]), ("a",["u","w"]), ("b":["t"])] 

because value of "c" has 3 items in list, "a" has two, "b" has one. thinking along lines of:

sorted(d.items(),key=operator.methodcaller(len(),tuple[1]),reverse=true) 

or

sorted(d.items(),key=(string, stringlist):(len(stringlist),string),reverse=true) 

but both not seem work. still quite new sorting, help!

>>> d = {"a":["u","w"],"b":["t"],"c":["x","y","z"]} >>> sorted(d.items(), key=lambda x: len(x[1]), reverse=true) [('c', ['x', 'y', 'z']), ('a', ['u', 'w']), ('b', ['t'])] 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -