[Tutor] Curious dictionary printing

Dave Angel d at davea.name
Tue May 8 13:58:47 CEST 2012

On 05/08/2012 07:27 AM, Cranky Frankie wrote:
> On Mon, May 7, 2012 at 10:31 PM, bob gailer <bgailer at gmail.com> wrote:
>> Asking why does it not do what I want is not IMHO the best way to win
>> friends here.
> Good morning to you to, Bob.
> I see now that dictionaries in Python act like relational databases in
> that there is no inherent ordering. At first, when I simply wanted to
> print one entry and it came out in what looked like a random order,
> this didn't occur to me. A normal "dictionary" is in fact ordered.
> Sorry for the disturbance.

I wouldn't relate it to a "normal dictionary," but to other programming
constructs.  A dictionary is a mapping between key and value, where once
a key/value pair has been stored, it may be retrieved by supplying the
key.  There are two common implementations in various programming
languages and environments.  In C++, for example, the std::map is
ordered, and does a binary search for each lookup.  Thus the lookup is
O(log n).   Boost adds a unordered_map (it may be in the C++ standard by
now, but I haven't checked).  So it has a lookup time of O(1), which is

Sometimes an ordered map is more useful than the other, but an unordered
map is generally faster (especially for large maps), and works with
unorderable keys.

Since Python chose to include only one native to the language, I think
it made the better choice, especially since it uses it heavily for its
own namespace lookups.



More information about the Tutor mailing list