473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sorting a list of dictionaries by dictionary key

Hi there,

I am looking for a way to sort a list containing dictionaries.

This is my example list:
[{'Title': 'ABC', 'from_datetime': DateTime('2006/04/25 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'GHI', 'from_datetime':
DateTime('2006/03/10 12:45:00 GMT+2')}]

I want to sort the list by dictionary's key 'from_datetime' so the
sorted list should be:

[{'Title': 'GHI', 'from_datetime': DateTime('2006/03/10 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'ABC', 'from_datetime':
DateTime('2006/04/25 12:45:00 GMT+2')}]

Any idea how I can sort this list?
Thank you very much in advance,
Nico
May 3 '06 #1
4 2219
Nico Grubert wrote:
Hi there,

I am looking for a way to sort a list containing dictionaries.

This is my example list:
[{'Title': 'ABC', 'from_datetime': DateTime('2006/04/25 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'GHI', 'from_datetime':
DateTime('2006/03/10 12:45:00 GMT+2')}]

I want to sort the list by dictionary's key 'from_datetime' so the
sorted list should be:

[{'Title': 'GHI', 'from_datetime': DateTime('2006/03/10 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'ABC', 'from_datetime':
DateTime('2006/04/25 12:45:00 GMT+2')}]

Any idea how I can sort this list?


The common idiom for this is called decorate-sort-undecorate.

unsorted_list = [
{'Title': 'ABC',
'from_datetime': DateTime('2006/04/25 12:45:00 GMT+2')},
{'Title': 'DEF',
'from_datetime': DateTime('2006/04/18 12:45:00 GMT+2')},
{'Title': 'GHI',
'from_datetime': DateTime('2006/03/10 12:45:00 GMT+2')}]

decorated_list = [(d['from_datetime'], d) for d in unsorted_list]
decorated_list.sort()
sorted_list = [t[1] for t in decorated_list]

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 3 '06 #2
Nico Grubert wrote:
Hi there,

I am looking for a way to sort a list containing dictionaries.

This is my example list:
[{'Title': 'ABC', 'from_datetime': DateTime('2006/04/25 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'GHI', 'from_datetime':
DateTime('2006/03/10 12:45:00 GMT+2')}]

I want to sort the list by dictionary's key 'from_datetime' so the
sorted list should be:

[{'Title': 'GHI', 'from_datetime': DateTime('2006/03/10 12:45:00
GMT+2')}, {'Title': 'DEF', 'from_datetime': DateTime('2006/04/18
12:45:00 GMT+2')}, {'Title': 'ABC', 'from_datetime':
DateTime('2006/04/25 12:45:00 GMT+2')}]

Any idea how I can sort this list?


sort can take a comparaison function. which specification are
takes 2 elements
returns a negative value if elem1 is lesser than elem2
returns a positive value if elem1 is grater than elem2
returns zero if elem1 == elem2

you may write such fuction

def dictionary_datetime_sorter(d1, d2):
date1 = d1['from_datetime']
date2 = d2['from_datetime']
if date1 < date2: return -1
elif date1 > date2: return 1
else: return 0

you'll have to find out how to compare 2 DateTime

and then, in order to sort your list
your_dictionary_list.sort(dictionary_datetime_sort er)
Eric
--
« Voyons-voir : est-ce que ma question est vraiment conne ? Oh oui,
vraiment trop conne, je vais poster dans "premiers-pas", je posterai
dans "configuration" quand je serai moins con. »
-+-JLC - Guide du Neuneu d'Usenet - Bien configurer sa question -+-
May 3 '06 #3
Eric Deveaud wrote:
(snip)

sort can take a comparaison function.


The problem with it is that it may slow down things a lot...

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 3 '06 #4
bruno at modulix wrote:
Eric Deveaud wrote:
(snip)

sort can take a comparaison function.


The problem with it is that it may slow down things a lot...


point taken.

I have to purge my mind from the other programing languages I practice. ;-)

Eric
May 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shalendra chhabra | last post by:
Hi, I just had a tryst with python. I was wondering if python is good enough to do this kind of job -- for it has extensive support of string and pattern matching, ordering and list handling. ...
5
by: Rakesh | last post by:
Hi, For a particular problem of mine, I want to sort <key, value> pairs by its value. Eg: Input: A, 4 B, 5
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
8
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.