Sort a python dict

Posted by ensnare on Stack Overflow See other posts from Stack Overflow or by ensnare
Published on 2010-05-20T21:44:20Z Indexed on 2010/05/20 21:50 UTC
Read the original article Hit count: 323

Filed under:
|
|

I have a list of dictionaries:

[{'title':'New York Times', 'title_url':'New_York_Times','id':4},
 {'title':'USA Today','title_url':'USA_Today','id':6},
 {'title':'Apple News','title_url':'Apple_News','id':2}]

I'd like to sort it by the title, so elements with A go before Z:

[{'title':'Apple News','title_url':'Apple_News','id':2},
 {'title':'New York Times', 'title_url':'New_York_Times','id':4},
 {'title':'USA Today','title_url':'USA_Today','id':6}]

What's the best way to do this? Also, is there a way to ensure the order of each dictionary key stays constant, e.g., always title, title_url, then id?

Thank you.

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner