Reputation: 25539
I built a data structure like this:
{ level: [ event1, event2... ] }
The level
is one of the following: C(stands for critical), H(stands for high), M(stands for medium), L(stands for low).
I want to print all events in django template based on the level, C(critical) comes first, then H(high), M(medium), L(low). However, by default, which is what I have:
{% for level, events in dictionary.items %}
{% for event in events %}
do something with level, event
{% endfor %}
{% endfor %}
I got H(high) printed out first, then C(critical), etc. I want to ask: How can I loop a dictionary in particular order? Or should I convert it into other data structure? Thanks.
Edit: I think Steve's method works fine. It converts a dictionary into a list, each entry of the dictionary becomes a tuple:
[ (level1: [event1, event2 ...]), (level2: [event3, event4 ...]) ]
Upvotes: 0
Views: 212
Reputation: 22818
Dictionaries are not ordered, so you will need to convert them to a list of tuples first:
Sort them as follows:
level_values = {'C':0, 'H':1, 'M':2, 'L':3}
sorted_dictionary = sorted(dictionary.items(), key=lambda x: level_values[x[0]])
Then pass in your sorted_dictionary variable, and loop it in the same way as before:
{% for level, events in sorted_dictionary %}
{% for event in events %}
do something with level, event
{% endfor %}
{% endfor %}
Further explanation of the important statement:
sorted_dictionary = sorted(dictionary.items(), key=lambda x: level_values[x[0]])
dictionary.items()
gives you a list of tuples, representing your original dictionary. So instead of a dictionary like this:
{'A':[1,2,3], 'B',[4,5,6]}
if gives you a list of tuples, for each key/value pair in the dictionary:
[('A', [1,2,3]), ('B', [4,5,6])]
You can think of a tuple as an list that can't be changed (it's said to be 'immutable').
This list of tuples is then passed into the sorted
function. For each tuple in the list, sorted()
calls the lambda expression we supplied to ask for a sorting key. Our lambda expression simply takes the first element in the tuple (i.e. the severity value), and accesses the level_values
dictionary to find a sort value for it.
You can loop through the resulting sorted_dictionary
(which is a list of tuples) either tuple by tuple:
for value in sorted_dictionary:
print value[0]
print str(value[1])
or Python will let you automatically split the tuble into separate variables:
for severity, events in sorted_dictionary:
print severity
print str(events)
Upvotes: 1
Reputation: 3111
Python dict
s are not ordered. You'll need to convert it to a list, and sort it.
converted = [
('C', mydict['C']),
('H', mydict['H']),
('M', mydict['M']),
('L', mydict['L']),
]
Then in the template:
{% for level, events in converted %}
{% for event in events %}
do something with level, event
{% endfor %}
{% endfor %}
Or, with your original datastructure, just loop through each category:
{% for event in dictionary.C %}
do something with event, this is for Critical
{% endfor %}
{% for event in dictionary.H %}
do something with event, this is for High
{% endfor %}
{% for event in dictionary.M %}
do something with event, this is for Medium
{% endfor %}
{% for event in dictionary.L %}
do something with event, this is for Low
{% endfor %}
Upvotes: 1
Reputation: 3429
You need to convert it to some other data structure. The easiest way to do this will be, instead of a dictionary, to pass into your template an already ordered list of lists, [low_levels, medium_levels, high_levels]
or however you'd like it, and iterate over those. If you can, you might as well just use that as the representation of your events.
Upvotes: 1