I have dictionary which has {key : []} format. I need to prepare a list sorted based on the length of the value (length of the list). What's the best way to accomplish this?
-
A list of what? The values sorted by length?Yuval Adam– Yuval Adam2012-10-27 17:42:27 +00:00Commented Oct 27, 2012 at 17:42
-
Duplicate [sorting a list of dictionary values by date in python][1]? [1]: stackoverflow.com/questions/652291/…Anton Bessonov– Anton Bessonov2012-10-27 17:44:21 +00:00Commented Oct 27, 2012 at 17:44
Add a comment
|
1 Answer
If you're looking for the values sorted by length, and assuming dict d:
lists_by_length = sorted(d.values(), key=len)
Also see Sort a Python dictionary by value
3 Comments
sarat
the list consists of objects from
Vertex class and the following errors are occured AttributeError: Vertex instance has no attribute '__len__'Yuval Adam
The the values aren't naive python
lists, or objects that implement __len__. How do you extract the Vertex object's length?sarat
Thanks for the answer, I added a global function to process the vertex class.