2

I have this list:

L = [{'status': 1, 'country': 'France'}, {'status': 1, 'country': 'canada'}, {'status': 1, 'country': 'usa'}]

How to sort this list by country (or by status) elements, ASC/DESC.

1
  • If these dictionaries are all matching (ie. they all have a 'status' key and a 'country' key, and no other keys), consider using instances of a class created by collections.namedtuple instead. It will make a lot of your code, including sorting, much simpler to read and write. See this link for details Commented May 14, 2013 at 9:55

3 Answers 3

9

Use list.sort() to sort the list in-place or sorted to get a new list:

>>> L = [{'status': 1, 'country': 'France'}, {'status': 1, 'country': 'canada'}, {'status': 1, 'country': 'usa'}]
>>> L.sort(key= lambda x:x['country'])
>>> L
[{'status': 1, 'country': 'France'}, {'status': 1, 'country': 'canada'}, {'status': 1, 'country': 'usa'}]

You can pass an optional key word argument reverse = True to sort and sorted to sort in descending order.

As a upper-case alphabet is considered smaller than it's corresponding smaller-case version(due to their ASCII value), so you may have to use str.lower as well.

>>> L.sort(key= lambda x:x['country'].lower())
>>> L
[{'status': 1, 'country': 'canada'}, {'status': 1, 'country': 'France'}, {'status': 1, 'country': 'usa'}]
Sign up to request clarification or add additional context in comments.

Comments

7
>>> from operator import itemgetter
>>> L = [{'status': 1, 'country': 'France'}, {'status': 1, 'country': 'canada'}, {'status': 1, 'country': 'usa'}]
>>> sorted(L, key=itemgetter('country'))
[{'status': 1, 'country': 'France'}, {'status': 1, 'country': 'canada'}, {'status': 1, 'country': 'usa'}]
>>> sorted(L, key=itemgetter('country'), reverse=True)
[{'status': 1, 'country': 'usa'}, {'status': 1, 'country': 'canada'}, {'status': 1, 'country': 'France'}]
>>> sorted(L, key=itemgetter('status'))
[{'status': 1, 'country': 'France'}, {'status': 1, 'country': 'canada'}, {'status': 1, 'country': 'usa'}]

3 Comments

I think we have to use str.lower here as dictionary wise canada must come before France.
@AshwiniChaudhary if that's required then your answer should be accepted ;) I just followed what the question asked so I'll leave this here for other people visiting
@jamylak: AshwiniChaudhary has right, canada should come before france in the first short, actually the short is correct with str.lower
2

Pulling the key out to a named function is a good idea for real code, because now you can write tests for it explicitly

def by_country(x):
    # For case insensitive ordering by country
    return x['country'].lower()

L.sort(key=by_country)

Of course you can adapt to use sorted(L, key=...) or reverse=True etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.