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
Post a Comment