Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51152 > unrolled thread
| Started by | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| First post | 2013-07-24 13:17 -0400 |
| Last post | 2013-07-25 05:52 +0000 |
| Articles | 2 — 2 participants |
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.
Re: Python 3: dict & dict.keys() Terry Reedy <tjreedy@udel.edu> - 2013-07-24 13:17 -0400
Re: Python 3: dict & dict.keys() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-25 05:52 +0000
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-07-24 13:17 -0400 |
| Subject | Re: Python 3: dict & dict.keys() |
| Message-ID | <mailman.5054.1374686249.3114.python-list@python.org> |
On 7/24/2013 12:34 PM, Chris Angelico wrote: > 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. Both were considered and I think there were and are two somewhat-linked practical reasons. First, iterating over keys in more common than iterating over items. The more common one should be the default. Second, people ask much more often if 'key' is in dict than if 'key, value' is in dict. This is true as well for keyed reference books such as phone books, dictionaries, encyclopedias, and for the same reason. This is coupled with the fact that the default meaning of 'item in collection' is that iterating over 'collection' eventually produces 'item' or a value equal to 'item'. -- Terry Jan Reedy
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-07-25 05:52 +0000 |
| Message-ID | <51f0bd32$0$29971$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #51152 |
On Wed, 24 Jul 2013 13:17:12 -0400, Terry Reedy wrote:
> On 7/24/2013 12:34 PM, Chris Angelico wrote:
>
>> 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.
>
> Both were considered and I think there were and are two somewhat-linked
> practical reasons. First, iterating over keys in more common than
> iterating over items. The more common one should be the default.
>
> Second, people ask much more often if 'key' is in dict than if 'key,
> value' is in dict. This is true as well for keyed reference books such
> as phone books, dictionaries, encyclopedias, and for the same reason.
> This is coupled with the fact that the default meaning of 'item in
> collection' is that iterating over 'collection' eventually produces
> 'item' or a value equal to 'item'.
That second point was the deciding factor when direct iteration over
dicts was added. has_key() was deprecated in favour of "key in dict", and
that pretty much forced iteration to go over keys by default. The
reasoning is, "x in y" ought to be equivalent to:
for tmp in y:
if x == tmp: return True
return False
There's probably even a PEP about this, if anyone is less lazy/busy and
can be bothered looking for it.
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web