467,880 Members | 1,246 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,880 developers. It's quick & easy.

sort a dictionary by keys in specific order

hi i have a normal dictionary with key and value pairs. now i wanna
sort by the keys BUT in a specific order i determine in a list !? any
ideas

dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}

list = [key2, key3, key1]

May 26 '06 #1
  • viewed: 7195
Share:
8 Replies
"spohle" <sp****@gmail.com> writes:
dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}

list = [key2, key3, key1]


Is this what you want?

dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}
keys = ['key2', 'key3', 'key1']
items = [dic[k] for k in keys]
print items
May 26 '06 #2
> hi i have a normal dictionary with key and value pairs. now i wanna
sort by the keys BUT in a specific order i determine in a list !? any
ideas

dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}

list = [key2, key3, key1]


1) it's bad practice to shadow the list() command...funky stuff
can happen.

2) I presume your list is a list of strings, not of references:

order = ['key2', 'key3', 'key1']

3) As a dictionary is an unordered collection, I presume you want
a resulting list of key/value pairs.

If both #2 and #3 hold, you can use

results = [(k,dic[k]) for k in order]

which will return a list of tuples in the desired key order.

-tkc

May 26 '06 #3
how do i get the result back into the dictionary ?

May 26 '06 #4
> how do i get the result back into the dictionary ?

Well, if you must, if you've just got the results in my
previous post, you can take them and shove them back into a
dict with

results = [('key1','value1'),('key2','value2)]
newDict = dict(results)

If you're not doing anything with that the resulting list of
ordered key/value pairs (such as inserting, printing,
whatever), then skip the whole matter:

newDict = dic

:)

HOWEVER...as I noted, a dictionary is an INHERENTLY UNSORTED
COLLECTION. There are some wrapper-classes around that will
feign sortedness if you want them, but they may not handle
your custom sortedness constraint.

There are few reasons for a sorted dict. Most of them
regard displaying them. If this is the case, just do the
sorting/selection of the items before you print:

print "\n".join([dic[k] for k in order])

Other reasons might involve dependancies, where some process
requires that you access the keys in a particular order.
Just order them before you call the process.

-tkc


May 26 '06 #5
i write the dict out to a file, not with file methods but rather with
an inhouse python code. unfortunatly the order plays a big role for
that.

May 26 '06 #6
Tim Chase a écrit :
(snip)
list = [key2, key3, key1]
1) it's bad practice to shadow the list() command...


s/command/type/
funky stuff can happen.


indeed, if you shadow it with a non-compatible object !-)

May 26 '06 #7
spohle a écrit :
how do i get the result back into the dictionary ?

Python dicts (like almost any known hash-table) are *not* ordered. If
you need an ordered dict, roll your own - this is quite easy.
May 26 '06 #8
spohle wrote:
hi i have a normal dictionary with key and value pairs. now i wanna
sort by the keys BUT in a specific order i determine in a list !? any
ideas

dic = {'key1':'value1', 'key2':'value2', 'key3':'value3'}

list = [key2, key3, key1]


You could use the seqdict package at
http://home.arcor.de/wolfgang.grafen...s/Modules.html

Regards

Wolfgang
May 29 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Afanasiy | last post: by
2 posts views Thread by Tim Daneliuk | last post: by
125 posts views Thread by Raymond Hettinger | last post: by
9 posts views Thread by Tim N. van der Leeuw | last post: by
4 posts views Thread by Tim Henderson | last post: by
6 posts views Thread by max sharma | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.