Reputation: 895
I have a dictionary that looks like this:
diction = {1: {'Type': 'Cartoon', 'Time': 8:00},
2: {'Type': 'Movie', 'Time': 19:30},
3: {'Type': 'Show', 'Time': 16:00}}
So it's a dictionary containing dictionaries. I've read that a dictionary cannot be sorted so I'm trying to dynamically add these inner dictionaries to diction as they are created. And I need them sorted by time. Earliest time first. So for example, when it comes time for the program to add the inner dictionary of type show to the dictionary, it adds it inbetween cartoon and movie based on their time values. Basically, is there any way to push items into the middle of a dictionary? I know you can pop them off but I can't find any push equivalent
Upvotes: 3
Views: 2626
Reputation:
As Junuxx suggested: "Wouldn't it be better if you used a list of dictionaries instead?" why you don't try with:
diction = [{'Type': 'Cartoon', 'Time': 8:00},
{'Type': 'Movie', 'Time': 19:30},
{'Type': 'Show', 'Time': 16:00}]
List are ordered, dictionary, as you said, are not because they are meant to be "explored" with a hash key.
Now you can simply retrive your inner dictionay with diction[0], diction[1] etc, and you will be sure that it will give the exact element in the list, ordered by append time
Upvotes: 3