Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8269007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:04:41+00:00 2026-06-08T06:04:41+00:00

I built a data structure like this: { level: [ event1, event2… ] }

  • 0

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 ...]) ]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:04:42+00:00Added an answer on June 8, 2026 at 6:04 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would one test whether data structure built properly? I'm implementing kind of modified
I realize this sort of data structure is better done with built in list
I'm searching for an algorithm which will convert a data structure like the following
Is there a built-in data structure in Java to represent a Crit-bit tree? Or
I'm looking for a Java built-in data structure that would be the best at
Say I have a data structure like a Dictionary<string, Dictionary<string, int>> or similar, and
I have a photoblog built on CakePHP 2.0 with a data structure that looks
I would like to build an in-memory table data structure with 4 columns so
If I notice that a hash table (or any other data structure built on
I am trying to build a Quadtree data structure(or let's just say a tree)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.