0

I've the following code:

for i in xrange(len(arr_id)):
    list.append({'id': arr_id[i], 'porcentaje' : 0, 'estado' : 'En funcionamiento'})

If I print list it seems I get a alphabetically ordered list:
[{'estado': 'En funcionamiento', 'id': 0, 'porcentaje': 0}]

Which is not in the same order I define it:
[{'id': 0, 'porcentaje': 0, 'estado': 'En funcionamiento',}]

What can I do to maintain the desired order?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Avión
  • 7,963
  • 11
  • 64
  • 105
  • 8
    Python dictionaries have no set order. Why do you need the dictionary key order to be maintained? – Martijn Pieters Mar 03 '14 at 10:50
  • You're sorting a list where there is only one item, it's a dictionnary. Python dictionnary does not have an 'order' as Martinjn pointed out. – Depado Mar 03 '14 at 10:51
  • Because this createst a `json` and I need an exact order to read them in Java. – Avión Mar 03 '14 at 10:51
  • 2
    @Borja: JSON objects have no order either. Even if you sorted the keys, Java would be free to decode them in a different order. – Martijn Pieters Mar 03 '14 at 10:51
  • @Borja: use a list of key-value tuples instead if order is important. – Martijn Pieters Mar 03 '14 at 10:52
  • You *can* maintain the order by using a data structure that keeps track of order on top of a regular dict, but usually, you don't need it. Consider whether this is just surprising or actually a problem, and remember that your dict doesn't need to keep its keys in any particular order for you to iterate through its keys in an order you choose. – user2357112 Mar 03 '14 at 10:53

2 Answers2

2

First, the list will maintain the order, but the dictionaries inside the list won't, because dictionary are by definition unordered. There is a datatype called OrderedDict that does retain order, but I doubt you really need that.

A few other comments:

Don't use list as a variable name.

Don't iterate as if Python were C. You can iterate directly:

for item in arr_id:
    mylist.append({'id': item, 'porcentaje' : 0, 'estado' : 'En funcionamiento'})

or even use a list comprehension:

mylist = [{'id': item, 'porcentaje' : 0, 'estado' : 'En funcionamiento'}
          for item in arr_id]
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

First of all, you're not dealing with a list right now. You're dealing with a dictionnary nested into a list (which is pointless since there is only one item in your list)

You can't "sort" a dictionnary, because it's too easy to get it's value from a key. In your case :

>>> x = {'estado': 'En funcionamiento', 'id': 0, 'porcentaje': 0}
>>> x['estado']
b"En funcionamiento"

So there is no need to sort a dictionnary.

Though you can show a sorted representation of your dict. It would be only for printing purpose though.

import operator
x = {'estado': 'En funcionamiento', 'id': 0, 'porcentaje': 0}
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1))

Or as other answers pointed out, you could use an OrderedDict to retain the order of your dict.

Depado
  • 4,811
  • 3
  • 41
  • 63