Dictionary .keys() and .values() should return a set [withPython3000 in mind]

cmdrrickhunter@yaho.com conrad.ammon at gmail.com
Mon Jul 3 05:49:29 EDT 2006


You bring up a good point.  the "for x in d:" notation is a relativly
new construction, "for x in d.keys()" is much older.  Some of the value
of d.keys() goes away because we have this new construction, but
there's some reasons to keep it around:
1. Consitency.  You can get the values, you can get the (key, value)
pairs.  it'd be odd not to be able to get the keys
2. Choices.  if d.keys() is a FAST operation, then you can then use
that to create any structure you want.  For example, if you want a set,
  set(d.keys()) only requires you to create a set.  If d.keys() created
an independant list, python would first need to create a list, then
create a set.
Paul Rubin wrote:
> "Delaney, Timothy (Tim)" <tdelaney at avaya.com> writes:
> > If you want an independent data set, you have to take a snapshot. For
> > the above, that's doing:
> >
> >     k0 = list(d.keys())
>
> I don't understand.  Why have .keys() at all, if it doesn't get you
> an independent data set?  If all you want is to iterate through the
> dict, you can already do that:
> 
>   for k in d: ....




More information about the Python-list mailing list