Python 3: dict & dict.keys()
ethan at stoneleaf.us
Wed Jul 24 20:31:58 CEST 2013
On 07/24/2013 10:23 AM, Stefan Behnel wrote:
> 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?
>> 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.
I started this thread for two reasons:
1) Increase awareness that using `list(dict)` is a cross-version replacement for `dict.keys()`
2) Hopefully learn something about when a view is useful.
So far #2 is pretty much a failure. Only one use-case so far (and it feels pretty rare). But hey, I have learned that
while some set operations are allowed (&, ^, |, .isdisjoint()), others are not (.remove(), .discard(), .union(), etc.).
The old .keys(), .values(), and .items() (and their .iter...() variations) did something commonly useful. Of what
common use are these views?
More information about the Python-list