dict = no ordered keys = no slicing

Antoon Pardon apardon at forel.vub.ac.be
Sat Jul 15 10:50:35 EDT 2006


On 2006-07-14, Nick Vatamaniuc <vatamane at gmail.com> wrote:
>>I have a tree class, a tree acts like a dictionary, but when you
>> iterate over it, it always iterates over the keys in order.
>
> Antoon,
>
> First of all there is a distinction between ordered and un-ordered data
> types. You can only slice ordered data types. Lists and tuples are
> ordered while the keyset (note the _set_ part) of a dictionary is a set
> - it is un-ordered and consists of unique elements, the keys.

That doesn't has to be. Let as talk about a mapping. A mapping is
a way to associate a key with a value. Now one way to implement
a mapping is a hash table, this is how python dictionaries
are implemented. 

Now I have an other mapping implementation, look at:

  http://www.pardon-sleeuwaegen.be/antoon/avltree.html

Now this mapping type has as a property that the order
of the keys is somehow stored with it, so that each
time you call the keys, values, or items methods, you
will get a list according the order of the keys. (The
same goes for the itervariants).

> Besides, I don't really understand what you mean by saying "a tree acts
> like a dictionary"? You don't really iterate over the dictionary
> because the keys are not in order! Remeber that. The confusing part is
> that ks=dic.keys() will return a list so that makes you think ks[0] is
> somehow first for some reason, but it shouldn't be.

My module is implemented to have that property. It is called a tree
because the implementation is a tree. May be with the API provided
you think a tree is not a good name for this class and you may
have a point there, but that is not what this discussion is about.

> You see, dictionaries were there in Python before sets(), that is why
> when sets are supposed to be used a dictionary or list is used. keys()
> is one of these example. Hopefully this will change in P3K so that the
> key_set_ of a dictionary is a set() so people don't get confused.

And what do you think should be the result of keys of my Tree class?

-- 
Antoon Pardon



More information about the Python-list mailing list