Help with sets

Ethan Furman ethan at stoneleaf.us
Mon Oct 11 08:39:08 EDT 2010


Lawrence D'Oliveiro wrote:
> In message <8h9ob9FkurU1 at mid.individual.net>, Gregory Ewing wrote:
> 
>>Lawrence D'Oliveiro wrote:
>>
>>>Did you know that applying the “set” or “frozenset” functions to a dict
>>>return a set of its keys?
>>
>>>Seems a bit dodgy, somehow.
>>
>>That's just a consequence of the fact that dicts produce their
>>keys when iterated over, and the set constructor iterates over
>>whatever you give it.
> 
> Hmm. It seems that “iter(<dict>)” iterating over the keys has been around a 
> long time. But a dict has both keys and values: why are language constructs 
> treating them so specially as to grab the keys and throw away the values?

What's so special about it?  If you want the value, ask for it; 
iterating over the dict without asking specifically for the values does 
not give them.  Furthermore, if you did get the key:value pairs how 
would you store them in a set such that you could query the set to see 
if a key were there?

~Ethan~



More information about the Python-list mailing list