0

I have a dictionary:

a = {"w1": "wer", "w2": "qaz", "w3": "edc"}

When I try to print its values, they are printed from right to left:

>>> for item in a.values():
    print item,


edc qaz wer

I want them to be printed from left to right:

wer qaz edc

How can I do it?

Levon
  • 138,105
  • 33
  • 200
  • 191
alwbtc
  • 28,057
  • 62
  • 134
  • 188
  • This appears to be related: http://stackoverflow.com/questions/364519/in-python-how-to-i-iterate-over-a-dictionary-in-sorted-order – dmh May 20 '12 at 16:30

5 Answers5

9

You can't. Dictionaries don't have any order you can use, so there's no concept of "left to right" with regards to dictionary literals. Decide on a sorting, and stick with it.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • @alwbtc that's an implementation issue which is not safe to depend on (ie it may not always do this in that order) – Levon May 20 '12 at 16:37
  • 2
    It doesn't "prefer to write values from right to left". It writes the values in an arbitrary order that depends on the exact value of the keys and on implementation details, that **just happens**, in this particular case, to be the reverse of the order you used when you created the dict. – Karl Knechtel May 20 '12 at 17:06
4

You can use collections.OrderedDict (python 2.7 or newer -- There's an ActiveState recipe somewhere which provides this functionality for python 2.4 or newer (I think)) to store your items. Of course, you'll need to insert the items into the dictionary in the proper order (the {} syntax will no longer work -- nor will passing key=value to the constructor, because as others have mentioned, those rely on regular dictionaries which have no concept of order)

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • +1 Whenever anyone starts asking for dicts to output in something other than arbitrary or sorted key order, an `OrderedDict` or similar custom class is what they're after. – Matthew Trevor May 21 '12 at 07:02
3

Assuming you want them in alphabetical order of the keys, you can do something like this:

a = {"w1": "wer", "w2": "qaz", "w3": "edc"}  # your dictionary

keylist = a.keys()   # list of keys, in this case ["w3", "w2", "w1"]
keylist.sort()       # sort alphabetically in place, 
                     #  changing keylist to ["w1", "w2", w3"]

for key in keylist:
    print a[key]     # access dictionary in order of sorted keys
Junuxx
  • 14,011
  • 5
  • 41
  • 71
0

as @IgnacioVazquez-Abrams mentioned, this is no such thing as order in dictionaries, but you can achieve a similar effect by using the ordered dict odict from http://pypi.python.org/pypi/odict

also check out PEP372 for more discussion and odict patches.

nye17
  • 12,857
  • 11
  • 58
  • 68
0

Dictionaries use hash values to associate values. The only way to sort a dictionary would look something like:

dict = {}
x = [x for x in dict]
# sort here
y = []
for z in x: y.append(dict[z])

I haven't done any real work in python in a while, so I may be a little rusty. Please correct me if I am mistaken.

motoku
  • 1,571
  • 1
  • 21
  • 49