0

So i have initialized a forloop that iterates over a range of objects, where range starts from 1 and ends at the maximum number of fields a model has

for count in range(1, len(field_list)):
    f_val = getattr(instance, field_list[count])
    field_values[field_list[count]] = f_val
    print "field_values: ", field_values            

The f_val variable calculates the field value using model_instance.field_name and inserts the field name and field value as key value pairs in 'field_values' dictionary. When i print field_values i get:

field_values:  {'descr': u'ggg'}
field_values:  {'descr_en': u'sg', 'descr': u'ggg'}
field_values:  {'notes': u'ddddf', 'descr_en': u'sg', 'descr': u'ggg'}

so what i want to do now is reverse the order of the key value pairs such that i have {'descr': u'ggg', 'descr_en': u'sg', 'notes': u'ddddf'}. Thus, I used reversed() in my initial loop (for count in reversed(range(1, len(field_list))):) but my dictionary although it started with the last key value pair instead of the first still the final output was the same as in the beginning.

field_values:  {'notes': u'ddddf'}
field_values:  {'notes': u'ddddf', 'descr_en': u'sg'}
field_values:  {'notes': u'ddddf', 'descr_en': u'sg', 'descr': u'ggg'}

So I would like to know why this is happening?? does it sort my dictionary by default?? And if this approach does not work what other approaches could i follow?

Peter Andre
  • 81
  • 1
  • 11

2 Answers2

2

Dictionaries are unordered, so it doesn't matter what order you insert items into them.

The standard library has an ordered version of the dictionary as well, collections.OrderedDict. Maybe you can use that.

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
2

Dictionaries are unordered data types indexed by their keys (documentation found here) so it doesn't matter what order you add keys to your dict.

There are some great questions on sorting a dictionary by value and sorting a dictionary by key using OrderedDict.

Community
  • 1
  • 1
Ewan
  • 14,592
  • 6
  • 48
  • 62
  • Though why you'd want to do this other than aesthetics (waste of computation power IMO) I don't know... – Ewan Feb 12 '14 at 09:31
  • i am doing this in django and i have lots of models with lots of fields and thus i want to render everything in an orderly fashion in my templates. So yeah you could say it is for the aesthetics of organising... to aid the user in identifying the fields more quickly. – Peter Andre Feb 12 '14 at 11:38
  • Anyway the anwser you provided was really helpful! Thanks a lot! – Peter Andre Feb 12 '14 at 11:38
  • 1
    Ah this makes a bit more sense @per7inac1ousQ. One thing you could do is actually return this unordered and then order it client side with Javascript (see [this question](http://stackoverflow.com/questions/8837454/sort-array-of-objects-by-single-key) about ordering objects). This has the ability that the user may be able to order by other keys (if it's in a table or similar for example). Just a thought... – Ewan Feb 12 '14 at 12:13
  • Indeed that is a nice thought, moving part of the processing on the client side, thus boosting the performance. However, I am more concerned about the reliability of the data (JavaScript is not very dependable on this end) so I will see at a later stage to what extend I will implement this approach. – Peter Andre Feb 12 '14 at 15:26