Ordering in the printout of a dictionary

M

Mok-Kong Shen

Could someone kindly explain a phenomenon in the following where:

(1) I first typed in a dictionary but got a printout in a reordered
form.

(2) I then typed in the reordered form but got a printout in the
order that I typed in originally in (1).

That is, there is no stable "standard" ordering. Why is that so?

Is there a way to force a certain ordering of the printout or else
somehow manage to get at least a certain stable ordering of the
printout (i.e. input and output are identical)?

Thanks in advance.

M. K. Shen
 
C

Chris Angelico

Could someone kindly explain a phenomenon in the following where:

(1) I first typed in a dictionary but got a printout in a reordered
form.

(2) I then typed in the reordered form but got a printout in the
order that I typed in originally in (1).

That is, there is no stable "standard" ordering. Why is that so?

A dictionary is simply a mapping from keys to values. It has no ordering.
Is there a way to force a certain ordering of the printout or else
somehow manage to get at least a certain stable ordering of the
printout (i.e. input and output are identical)?

Yes; instead of simply printing it out (which calls repr()),
explicitly iterate over it, like this:

def display(d):
return '{'+','.join('%r: %r'%(key,d[key]) for key in sorted(d))+'}'
{'label': 3, 'left child': 1, 'parent': 0, 'right child': 2}

That will be consistent, and will also always be sorted, which will
probably be what you want for human-readable display. At least, it's
consistent as long as the keys all sort consistently, which they will
if you use simple strings. Other types of keys may not work, and in
fact mixing types may cause an exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in display
TypeError: unorderable types: str() < bool()

But for strings, this is the easiest way to get what you're looking for.

ChrisA
 
J

John Gordon

Yes; instead of simply printing it out (which calls repr()),
explicitly iterate over it, like this:
def display(d):
return '{'+','.join('%r: %r'%(key,d[key]) for key in sorted(d))+'}'

You could also use the OrderedDict type, which is subclass of dict that
preserves insertion order.
 
C

Chris Angelico

I would say using pprint.pprint is even easier and it works with your
failing example:

{True: 1, 'Hello': 2}

True. I could try to say that I prefer to offer the simpler approach
rather than encourage people to automatically reach for the nearest
hammer and hope it's right, but the fact is... I completely forgot
about pprint :)

ChrisA
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top