Dictionary .keys() and .values() should return a set [with Python 3000 in mind]

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Jul 2 05:43:57 EDT 2006


Paddy:
> Mind you, Never rely on that implied ordering. Always use items().

Using dict.items() is probably better, but the manual says:

>If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): "pairs = zip(a.values(), a.keys())". The same relationship holds for the iterkeys() and itervalues() methods:<

Is this going to change?


dict.keyset() seems nice, but you usually don't want to make a too much
big API.
Keeping APIs small is very important in Python, otherwise you need the
manual to write code.
I think a better solution to solve such key set problems is to optimize
Python itself, so Python computes set(dict) really fast (it can just
"copies" the hash of the dict).

Bye,
bearophile




More information about the Python-list mailing list