Python 3: dict & dict.keys()
ethan at stoneleaf.us
Wed Jul 24 17:57:11 CEST 2013
On 07/24/2013 05:51 AM, Oscar Benjamin wrote:
> On Jul 24, 2013 7:25 AM, "Peter Otten" <__peter__ at web.de <mailto:peter__ at web.de>> wrote:
>> 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.
>> To me it looks like views are a solution waiting for a problem.
> What do you mean? Why would you want to create a temporary list just to iterate over it explicitly or implicitly (set,
> sorted, max,...)?
You wouldn't. But you don't need .keys() for that either as you can just use the dict itself.
My point is that in 2.x .keys() did something different from the dict, while in 3.x it appears to me that they are the same.
Peter's point is that in the stdlib the new functionality of .keys() is never used, not even once.
More information about the Python-list