[Tutor] Dictionary Elements

Magnus Lycka magnus@thinkware.se
Wed, 16 Oct 2002 16:33:10 +0200


At 06:43 2002-10-16 -0500, Don Arnold wrote:
>You _can_ do it by calling the dictionary's items() method, which gives you
>a list of key/value pairs:
>...
>But you shouldn't really want to. A dictionary is an unordered collection,
>so the value of items()[2] can change as things are added or removed from
>the dictionary.

Don is a bit too subtle here I think. What he
should have said is:

NEVER EVER EVER EXPECT THE ELEMENTS IN A DICTIONARY
TO COME OUT IN A PARTICULAR ORDER!!!

Even if you never change your dictionary, and even
if you do hundreds of tests, and it always behave
as you expect, you shouldn't be surprised if the
order has been changed at another time when you
run the program. A dictionary is NOT a sequence.

Sure, it might work for a year, but it might fail
if you change Python version, operating system,
run it with Jython or what ever. It's just as safe
as putting three letters in a mail box on the same
day, and expect that the recipient will read them
in the order you placed them in the box.



--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se