2

I have a python dictionary that I want to sort according to name:

location_map_india = {
  101: {'name': 'Andaman & Nicobar Islands', 'lat': 11.96, 'long': 92.69, 'radius': 294200},  
  108: {'name': 'Andhra Pradesh', 'lat': 17.04, 'long': 80.09, 'radius': 294200},
...
}

It doesn't come out the way I expect it. I tried:

location_map_india = sorted(location_map_india.iteritems(), key=lambda x: x.name)

The above didn't work. What should I do?

Update

The following was allowed and behaved as expected. Thanks for all the help. Code:

location_map_india = sorted(location_map_india.iteritems(), key=lambda x: x[1]["name"])

Template:

{% for key,value in location_map_india %}<option value="{{key}}" >{{value.name}}</option>{% endfor %}
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • 5
    Dictionaries don't have an order and thus can't be sorted. What is your goal here? – Tim Pietzcker Sep 21 '11 at 17:06
  • 1
    Depending on your Python Version you could use an OrderedDict instead of a standard dictionary. It's in the collections module. Otherwise, explain your intent for better suggestions on approach. – g.d.d.c Sep 21 '11 at 17:09
  • Duplicate: http://stackoverflow.com/q/364519/346587 – Paul Sep 21 '11 at 17:10
  • Thanks for the suggestions. I had a data structure and I would like to order it by name in that natural order. It seems it's not that simple. – Niklas Rosencrantz Sep 21 '11 at 17:35
  • use `.items()` instead if `.iteritems()` to assure compatibility with Python 3.x – Remi Oct 01 '11 at 17:22

4 Answers4

6

You are close. Try:

location_map_india = sorted(location_map_india.iteritems(), key=lambda x: x[1]["name"])

but the result would be a list not a dict. dict is orderless.

Avaris
  • 35,883
  • 7
  • 81
  • 72
  • I understand. But I want the key/value pairs and in order. What should I do? – Niklas Rosencrantz Sep 21 '11 at 18:15
  • 1
    What I wrote will give you that. It will be list of (key,value) tuples sorted according to "name". – Avaris Sep 21 '11 at 18:21
  • Thanks. I could make it loop but I couldn't make it print the info: `{% for a in location_map_india %}{% endfor %}` – Niklas Rosencrantz Sep 21 '11 at 21:19
  • 1
    I have no idea what language that is. But if you want to access elements after the sorting I gave, use a[0] for key and a[1] for value. – Avaris Sep 21 '11 at 22:24
  • Ok. The language is django templates where I pass the variable to the template and then iterate over it. In this case iterating becomes rather difficult since i don't know how to access the elements in django templates. – Niklas Rosencrantz Sep 22 '11 at 02:09
  • @Niklas R: django part beats me. I'm not familiar with it. Just looked the doc for syntax and might i suggest a.0 or a.1 or a.1.name? – Avaris Sep 22 '11 at 15:57
3

If you're using python 2.7 or superior, take a look at OrderedDict. Using it may solve your problem.

>>> OrderedDict(sorted(d.items(), key=lambda x: x[1]['name']))
wleao
  • 2,316
  • 1
  • 18
  • 17
1

you should try

location_map_india = sorted(location_map_india.items(), key=lambda x: x[1]['name'])
Xavier Combelle
  • 10,968
  • 5
  • 28
  • 52
1

If you are using a dictionary that must have order in any way, you are not using the correct data structure.

Try list or tuples instead.

krenel
  • 776
  • 6
  • 7