sort a dictionary by keys in specific order

S

spohle

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]
 
P

Paul Rubin

spohle said:
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
 
T

Tim Chase

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
 
T

Tim Chase

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
 
S

spohle

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.
 
B

Bruno Desthuilliers

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top