Python 3: dict & dict.keys()

Stefan Behnel stefan_ml at
Wed Jul 24 19:23:47 CEST 2013

Peter Otten, 24.07.2013 08:23:
> Ethan Furman wrote:
>> So, my question boils down to:  in Python 3 how is dict.keys() different
>> from dict?  What are the use cases?
> I just grepped through /usr/lib/python3, and could not identify a single 
> line where some_object.keys() wasn't either wrapped in a list (or set, 
> sorted, max) call, or iterated over.

In case that directory mainly consists of the standard library, then you
shouldn't forget that most of the code in there predates Python 3 by ages
and was only adapted to work with the new syntax/semantics, not rewritten
in a "better" way.

Even if it's not just the stdlib, that argument still holds. There is still
fairly little code out there that was specifically written for Py2.6+, as
opposed to just being adapted.

> To me it looks like views are a solution waiting for a problem.

They reduce the API overhead. Previously, you needed values() and
itervalues(), with values() being not more than a special case of what
itervalues() provides anyway. Now it's just one method that gives you
everything. It simply has corrected the tradeoff from two special purpose
APIs to one general purpose API, that's all.


More information about the Python-list mailing list