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 8789205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:20:20+00:00 2026-06-13T22:20:20+00:00

I am using Python and Django and messing around with returning JSON objects as

  • 0

I am using Python and Django and messing around with returning JSON objects as Python dictonaries, but am not content because I can’t iterate through my dictionary’s elements in the order they were inserted.

If I create a dictionary as follows:

measurements = {
  'units': 'imperial',
  'fit': request.POST[ 'fit' ],
  'height': request.POST[ 'height' ],
  'weight': request.POST[ 'weight' ],
  'neck': request.POST[ 'neck' ],
  # further elements omitted for brevity
}

I can try iterating through it like:

for k,v in measurements.iteritems():
  print k, 'corresponds to ', v

The result is:

shoulders corresponds to  shoulders_val
weight corresponds to  weight_val
height corresponds to  height_val
wrist corresponds to  wrist_val
...

I also tried using sorted(), which iterates through my elements by key alphabetically

bicep corresponds to  bicep_val
chest corresponds to  chest_val
fit corresponds to  fit_val
height corresponds to  height_val
...

I am new to Python. I am hoping to find some way to both reference my dictionary elements by named keys like measurements[‘units’] but still be able to iterate through these elements in the order they were created. I am aware that there is an ordered dictionary module out there, but I would like to stay away from nonstandard packages. Would any other standard Python data structures ( lists, arrays, etc. ) allow me to iterate in insertion order and reference values by named keys?

  • 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-13T22:20:22+00:00Added an answer on June 13, 2026 at 10:20 pm

    You can use a collections.OrderedDict to preserve insertion order if you’re using py2.7 or newer. This is a part of the standard library. For older versions, there’s an activestate recipe floating around that you could copy and use as part of your package/module. Otherwise, there’s nothing in the standard library that will do it.

    You could subclass dict yourself and make it so the it remembers the order things were inserted — storing the information in a list for instance — but that is overkill when something already exists in the standard library for newer versions and a recipe that you can copy/paste is readily available if you want to support old versions.


    Note that dictionary methods which accept dictionaries (__init__, update) won’t be sorted properly if you pass dictionaries to them:

    import collections
    dd = collections.OrderedDict({
      'units': 'imperial',
      'fit': 'fit' ,
      'height': [ 'height' ],
      'weight': [ 'weight' ],
      'neck': [ 'neck' ],
    })
    
    print( dd )  #Order not preserved
    
    
    #Pass an iterable of 2-tuples to preserve order.
    ddd = collections.OrderedDict([
      ('units', 'imperial'),
      ('fit', 'fit') ,
      ('height', [ 'height' ]),
      ('weight', [ 'weight' ]),
      ('neck', [ 'neck' ]),
    ])
    
    print( ddd ) #Order preserved
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was using python 2.5 and django 1.0.2. But I moved to python 2.6
I am using python and django and like it a lot. But than i
I've a GAE app in python using Django. I'm trying to fetch a json
I'm using Python/Django, but this is more about the data model and how I
I tried to add search fields in Django using python. Followings are the codes
hi i am working on a django python application using sqlite3 database. I have
I am using Google motion-charts in a web-site developed in python/django. Is there a
I'm using Django (and Python) for the first time, and I'm looking for the
In Django I am using two applications: python manage.py startapp books python manage.py startapp
I'm fairly new to both Django and Python. This is my first time using

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.