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


Groups > comp.lang.python > #51129

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'explicitly': 0.05; 'subject:Python': 0.06; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:/ 10': 0.09; 'subject:()': 0.09; 'wrapped': 0.09; 'python': 0.11; '24,': 0.16; 'benjamin': 0.16; 'iterated': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'set,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'example': 0.22; 'header:User-Agent:1': 0.23; '2.x': 0.24; 'either.': 0.24; 'looks': 0.24; '(or': 0.24; 'question': 0.24; 'header:X-Complaints-To:1': 0.27; 'am,': 0.29; 'code': 0.31; 'keys': 0.31; 'lists': 0.32; 'skip:m 30': 0.32; 'guess': 0.33; 'actual': 0.34; 'could': 0.34; 'problem.': 0.35; 'but': 0.35; 'picking': 0.36; "didn't": 0.36; 'so,': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'users': 0.40; 'how': 0.40; 'even': 0.60; 'most': 0.60; 'identify': 0.61; 'different': 0.65; 'temporary': 0.65; 'subject: & ': 0.68; 'jul': 0.74; '7:25': 0.84; 'ethan': 0.84; 'furman': 0.84; 'idiom': 0.84; 'lying': 0.84; 'oscar': 0.84; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Python 3: dict & dict.keys()
Date Wed, 24 Jul 2013 15:25:14 +0200
Organization None
References <51EF2AD8.3080105@stoneleaf.us> <ksnrr9$k4t$1@ger.gmane.org> <CAHVvXxQGCFJe7ud+mwh4zhnq5F7xvHJX1pCtGCjMaFtjBwY=iQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084a7c8.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5034.1374672304.3114.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1374672304 news.xs4all.nl 16009 [2001:888:2000:d::a6]:41778
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:51129

Show key headers only | View raw


Oscar Benjamin wrote:

> On Jul 24, 2013 7:25 AM, "Peter Otten" <__peter__@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,...)?

I mean I don't understand the necessity of views when all actual usecases 
need iterators. The 2.x iterkeys()/iteritems()/itervalues() methods didn't 
create lists either.

Do you have 2.x code lying around where you get a significant advantage by 
picking some_dict.viewkeys() over some_dict.iterkeys()? I could construct 
one

>>> d = dict(a=1, b=2, c=3)
>>> e = dict(b=4, c=5, d=6)
>>> d.viewkeys() & e.viewkeys()
set(['c', 'b'])

but have not seen it in the wild.

My guess is that most non-hardcore users don't even know about viewkeys(). 
By the way, my favourite idiom to iterate over the keys in both Python 2 and 
3 is -- for example -- max(some_dict) rather than 
max(some_dict.whateverkeys()).

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python 3: dict & dict.keys() Peter Otten <__peter__@web.de> - 2013-07-24 15:25 +0200

csiph-web