0

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?

2
  • A list of what? The values sorted by length? Commented Oct 27, 2012 at 17:42
  • Duplicate [sorting a list of dictionary values by date in python][1]? [1]: stackoverflow.com/questions/652291/… Commented Oct 27, 2012 at 17:44

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

the list consists of objects from Vertex class and the following errors are occured AttributeError: Vertex instance has no attribute '__len__'
The the values aren't naive python lists, or objects that implement __len__. How do you extract the Vertex object's length?
Thanks for the answer, I added a global function to process the vertex class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.