[Tutor] use of the newer dict types
Dave Angel
davea at davea.name
Fri Jul 26 00:37:53 CEST 2013
On 07/25/2013 05:49 PM, Jim Mooney wrote:
> If you do dict.keys() in 2.7 you get a list, which is quite handy. But
> if you do it in 3.3 you get this odd dict_keys type, for which I have
> yet to find a use, and which just requires an extra list step to use.
> The same for values. Since most changes from 2.7 to 3.3 were
> improvements, what am I missing here? What is the use of these types?
>
The difference is analogous to the difference between range and xrange
(in Python 2.x). The former is an iterator, rather than a list. If
you're just going to use it in a loop anyway, xrange is smaller and
faster. So 3.x just renamed xrange to range, and dropped the old range.
For dictionaries, instead of returning a list, keys() returns a
dictionary view. See
http://docs.python.org/3/library/stdtypes.html#dict-views
What's not clear to me is whether this dictionary view object is safe to
use when the dictionary is changing. The page referenced above seems to
say both yes and no.
--
DaveA
More information about the Tutor
mailing list