I don't think you're using the dictionary correctly, unless I am misunderstanding the question. I've tried asking for clarification in the comments, but that doesn't seem to be helping.
Dictionaries work on a key-value pair system. This means that you use a key, and not a traditional index (0, 1, 2, ...) to access the value.
For example, say that we have a dictionary call d
{ 'fruits': ['apple', 'banana', 'orange'], 'vegetables': ['carrot', 'potato'] }
In this dictionary, we access the array ['apple', 'banana', 'orange']
by calling d['fruits']
.
We would not talk about "ordering" the dictionary in the sense that we did sometime like this:
{ 'vegetables': ['carrot', 'potato'], 'fruits': ['apple', 'banana', 'orange'] }
That just wouldn't make any sense given the data type.
You may be asking about something different. If what you want to do is something like the following:
Input:
{ '1': ['x', 'y', 'z'], '2': ['x'], '3': ['x', 'y'] }
Output:
{ '1': ['x'], '2': ['x', 'y'], '3': ['x', 'y', 'z'] }
the way you would do this is by first creating an array of the keys, then an array of of the elements:
keys = list(d.keys())
elements = list()
for k in keys:
elements.append(d[k])
Then you will sort both of these using a sorting algorithm of your choice. Once these have both been sorted, you will iterate through the keys
list and add the corresponding values to the original dictionary.
for i in range(len(keys)):
d[keys[i]] = elements[i]
This will give you a new dictionary with the smallest key being paired with the smallest (by length) list. Optionally, sort the elements array in the opposite way from the keys array to have the largest key being paired with the smallest list.