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

Robert Kern robert.kern at gmail.com
Sat Jul 1 17:19:42 EDT 2006


vatamane at gmail.com wrote:
> The same thing goes for the values(). Here most people will argue that
> values are not necessarily unique, so a list is more appropriate, but
> in fact the values are unique it is just that more than one key could
> map to the same value.  What is 'seen' in dictionary is the mapping
> rule.  Also the values are not ordered and should not be indexable --
> they should be a set. Just as  the keys(), one ordered list of values
> from a dictionary on one machine will not necessarily be equal to
> another list of values an on another machine, while in fact, they
> should be.

This part is pretty much a non-starter. Not all Python objects are hashable.

In [1]: d = {}

In [2]: for i in range(1, 10):
    ...:     d[i] = range(i)
    ...:
    ...:

In [3]: set(d.values())
---------------------------------------------------------------------------
exceptions.TypeError                                 Traceback (most recent call 
last)

/Users/kern/<ipython console>

TypeError: list objects are unhashable


Also, I may need keys to map to different objects that happen to be equal.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list