0

So I know the way to sort a list of dict but I just cannot figure out how to get the index at the same time. Suppose I have a dict like this:

cities = [{'city': 'Harford', 'state': 'Connecticut'},
          {'city': 'Boston', 'state': 'Massachusetts'},
          {'city': 'Worcester', 'state': 'Massachusetts'},
          {'city': 'Albany', 'state': 'New York'},
          {'city': 'New York City', 'state': 'New York'},
          {'city': 'Yonkers', 'state': 'Massachusetts'}]

I can sort this dict by 'state' using:

new_cities = sorted(cities, key=itemgetter('state'))

And get:

    cities = [{'city': 'Harford', 'state': 'Connecticut'},
          {'city': 'Boston', 'state': 'Massachusetts'},
          {'city': 'Worcester', 'state': 'Massachusetts'},
          {'city': 'Yonkers', 'state': 'Massachusetts'},
          {'city': 'Albany', 'state': 'New York'},
          {'city': 'New York City', 'state': 'New York'}]

But how can I get the index of the list at the same time?

Jianli Cheng
  • 371
  • 1
  • 8
  • 17
  • 3
    The index of what? – MattDMo Jul 24 '16 at 00:46
  • 1
    Possible duplicate of [Python: Index a Dictionary?](http://stackoverflow.com/questions/4326658/python-index-a-dictionary) – kaitian521 Jul 24 '16 at 00:53
  • @MattDMo: Sorry for not being clear. The index of the elements in the dict that are being sorted – Jianli Cheng Jul 24 '16 at 03:03
  • @kaitian521: it is similar question, but different dict though. Mine is a list of dict – Jianli Cheng Jul 24 '16 at 03:05
  • @JianliCheng as indicated in the duplicate, dictionaries do not have indexes, as they are unordered. `{'state': 'Massachusetts', 'city': 'Boston'}` is ***exactly*** the same as `{'city': 'Boston', 'state': 'Massachusetts'}`. Does bunji's answer below solve your problem? If so, please upvote and accept it. If not, please [edit] your question and describe *exactly* what you're trying to do, with examples if possible, and why neither the duplicate nor the answer below work for you. – MattDMo Jul 24 '16 at 03:15
  • @MattDMo if it is duplicate, then answer will be same. I looked at the post, but the solution cannot solve my question. – Jianli Cheng Jul 24 '16 at 03:57
  • @JianliCheng the problem is that ***we don't know what your question actually is.*** Please [edit] your question and describe *exactly* what you're trying to do, with examples if possible, and why neither the duplicate nor the answer below work for you. – MattDMo Jul 24 '16 at 03:59

1 Answers1

2
new_cities = sorted(enumerate(cities), key=lambda x: x[1]['state'])

enumerating it first will give you the index of the original cities list which can then be sorted.

>>> new_cities
[(0, {'city': 'Harford', 'state': 'Connecticut'}),
 (1, {'city': 'Boston', 'state': 'Massachusetts'}),
 (2, {'city': 'Worcester', 'state': 'Massachusetts'}),
 (5, {'city': 'Yonkers', 'state': 'Massachusetts'}),
 (3, {'city': 'Albany', 'state': 'New York'}),
 (4, {'city': 'New York City', 'state': 'New York'})]
bunji
  • 5,063
  • 1
  • 17
  • 36
  • This actually solved my question. Could you please explain a little bit more about using `x[1]` and why it can return the index? – Jianli Cheng Jul 24 '16 at 04:08
  • Sure. Enumerating your list returns a list of tuples where the first element is the index and the second is one of your dictionary objects (see docs [here](https://docs.python.org/2/library/functions.html#enumerate) ). Now when you sort, you want to sort based on the second element in each tuple (the dictionary object not the index), so you tell the lambda function to look at x[1] (ie. the second element in each tuple). – bunji Jul 24 '16 at 14:47
  • Actually, the enumerate function returns an enumerate object but for your purposes it acts like a list of tuples. – bunji Jul 24 '16 at 14:48