Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51151 > unrolled thread

Re: Python 3: dict & dict.keys()

Started byStefan Behnel <stefan_ml@behnel.de>
First post2013-07-24 19:16 +0200
Last post2013-07-24 19:16 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python 3: dict & dict.keys() Stefan Behnel <stefan_ml@behnel.de> - 2013-07-24 19:16 +0200

#51151 — Re: Python 3: dict & dict.keys()

FromStefan Behnel <stefan_ml@behnel.de>
Date2013-07-24 19:16 +0200
SubjectRe: Python 3: dict & dict.keys()
Message-ID<mailman.5053.1374686212.3114.python-list@python.org>
Chris Angelico, 24.07.2013 18:34:
> On Thu, Jul 25, 2013 at 1:57 AM, Ethan Furman wrote:
>> On 07/24/2013 05:51 AM, Oscar Benjamin wrote:
>>> 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.
> 
> Side point: Why is iterating over a dict equivalent to .keys() rather
> than .items()? It feels odd that, with both options viable, the
> implicit version iterates over half the dict instead of all of it.
> Obviously it can't be changed now, even if .items() were the better
> choice, but I'm curious as to the reason for the decision.

The reason is that you can easily get at the values when iterating over the
keys, or simply decide not to care about them and be happy with the keys
only. Note that there are also many use cases that need all keys but not
all values. If iteration always returned an item tuple by default, many use
cases would have to resort to using .keys() in order to be efficient. And
for the simple case, you'd have to type more, either the additional .keys()
or the useless tuple unpacking. So, the reasoning is that iteration should
do the basic thing that still allows you to do everything, instead of doing
everything and pushing unnecessary work on the users by default.

Stefan

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web