Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'anyway.': 0.05; 'subject:Python': 0.06; 'completeness': 0.07; 'intermediate': 0.07; 'list?': 0.07; 'rename': 0.07; 'skip:` 10': 0.07; 'apis': 0.09; 'received:155': 0.09; 'subject:()': 0.09; 'api': 0.11; 'python': 0.11; 'language,': 0.12; 'thread': 0.14; '(key,': 0.16; 'api,': 0.16; 'cleanly': 0.16; 'dict': 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'failure.': 0.16; 'from:addr:jpmorgan.com': 0.16; 'iterable': 0.16; 'iteration': 0.16; 'iterators': 0.16; 'language?': 0.16; 'mean,': 0.16; 'previously,': 0.16; 'received:155.180': 0.16; 'received:159': 0.16; 'received:159.53': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'securities,': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'all.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'replacing': 0.19; 'stefan': 0.19; 'fit': 0.20; '>>>': 0.22; 'memory': 0.22; 'to:name:python-list@python.org': 0.22; 'creating': 0.23; 'decide': 0.24; 'looks': 0.24; 'question': 0.24; 'equivalent': 0.26; 'pass': 0.26; 'values': 0.27; 'header:In- Reply-To:1': 0.27; 'feature': 0.29; 'am,': 0.29; 'thus': 0.29; "doesn't": 0.30; 'restrict': 0.30; '(which': 0.31; 'gives': 0.31; '>>>>': 0.31; 'keys': 0.31; 'overhead': 0.31; 'received:169.254': 0.32; 'interface': 0.32; 'running': 0.33; 'totally': 0.33; 'common': 0.35; 'problem.': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'accuracy': 0.36; 'false': 0.36; 'i.e.': 0.36; 'returning': 0.36; 'shorter': 0.36; 'method': 0.36; 'charset:us-ascii': 0.36; 'received:169': 0.37; 'so,': 0.37; 'two': 0.37; 'list': 0.37; 'clear': 0.37; 'step': 0.37; 'being': 0.38; 'implement': 0.38; 'generic': 0.38; 'requiring': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'users': 0.40; 'how': 0.40; 'even': 0.60; 'chain': 0.60; 'information,': 0.61; 'simply': 0.61; 'first': 0.61; 'back': 0.62; 'making': 0.63; 'became': 0.64; 'more': 0.64; 'different': 0.65; 'purchase': 0.65; 'between': 0.67; 'useful.': 0.68; 'subject: & ': 0.68; 'subject': 0.69; 'legal': 0.71; 'increase': 0.74; 'special': 0.74; 'sale': 0.75; 'avoids': 0.84; 'case?': 0.84; 'ethan': 0.84; 'everything.': 0.84; 'furman': 0.84; 'protocol,': 0.84; 'received:169.254.8': 0.84; 'inefficient': 0.91; 'items,': 0.91; 'step.': 0.91; 'yourself,': 0.95 X-DKIM: OpenDKIM Filter v2.1.3 sf2.jpmchase.com r6OKYc4j015008 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1374698078; bh=12J3oQc5JAJklJDuCUWJ79b48eqVPjAwd67Cq44x8Rg=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:content-transfer-encoding:MIME-Version; b=BYm/vEMwU8D3IYK+bwoY/LGDB3Tf+lEw9VMtyRxhMhSltyzRfyDcEp9g5W/8Pr8U4 DUY5VsVBqzzor26KXgNAaiQYYOhFD8SZxGqM4nhbf404jjzZ36XR7IAhPkt+BrdCAo 8QyIK/J2rVd0IqGQYE7BkZ8YDsWstk/WkcIIzszk= X-AuditID: a97c8566-b7f628e000000303-9e-51f03a5b1af3 From: "Prasad, Ramit" To: "python-list@python.org" Subject: RE: Python 3: dict & dict.keys() Thread-Topic: Python 3: dict & dict.keys() Thread-Index: AQHOiJKOfYycRz3X90uM6iWVTW41zZl0ahEAgAAYd4D//8Wi4A== Date: Wed, 24 Jul 2013 20:34:28 +0000 References: <51EF2AD8.3080105@stoneleaf.us> <51F01D9E.2030903@stoneleaf.us> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.67.79.47] Content-Type: text/plain; charset="us-ascii" content-transfer-encoding: quoted-printable MIME-Version: 1.0 X-DLP-FWD: Yes X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrIKsWRmVeSWpSXmKPExsUye4uJsG601YdAg0N9ZhZHWq+xOjB6vJj/ hi2AMaqB0SYxLy+/JLEkVSEltTjZVsklszg5JzEzN7VIQVfBqyA3vyg9MU8vOT9XSSEzxVbJ REmhICcxOTU3Na/EVimxoCA1L0XJjksBA9gAlWXmKaTmJeenZOal2yp5BvvrWliYWuoaKtmF ZGQWK6TmJmbmKCRsZM84draRsWCiZMWOh5dZGhhPCHcxcnJICJhIbG+5zwxhi0lcuLeeDcQW EjjCKHHkjlYXIxeQfZhRYsX312wQzmZGib5bW8Cq2AQMJY6ffcIOYosImEq8XrmbFcQWFtCU 6Hr9mw0iriWxe9sLRgjbSaL79QuwehYBVYn5Zx6BbeYVCJF4eOgdM8SCZYwSv7p6mUASnECD HrzaDFbECHTe91NrwOLMAuISt57MZ4I4W0BiyZ7zUC+ISrx8/I8VwpaXeDHlJRtEvY7Egt2f oGxtiWULX0MtFpQ4OfMJSxcjB9BiNYl9621BTAkBPontbdUTGCVmIVk2C8mgWUgGzUIyaAEj yypGyeLc9AxDveKyIr3SYr2sgtzkjMTiVL281JJNjMCUsrKmNW0H492ltocYBTgYlXh4GX69 DxRiTSwrrsw9xCjBwawkwvso+HCgEG9KYmVValF+fFFpTmrxIcYqYKBNZJYSTc4Hpru8knhD YzMLA1NTQwMLQyMTqggrifM6r30WKCSQDkyq2ampBalFMMuZODilGhjzLy57IVf07OfK6yVM rPvfnwgIPtR22Wudqv9d24aXkoLLZS95vTnZ2XLx5/llch/CT51V/hK9V8tmHZvRcaZ/U/UO by06Mrd399y2h7vXG3zviRTbkvrcvK3xeR3Hkh8Ru/QO2K5YvmTOnQWNAeFaSS/Cjea+NZZL 9Dk1KzLJ1vuE3N0QdpWjSizFGYmGWsxFxYkAFaXRGYQDAAA= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374699269 news.xs4all.nl 15869 [2001:888:2000:d::a6]:48987 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51166 Stefan Behnel wrote: > Ethan Furman, 24.07.2013 20:31: > > 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() differ= ent > >>>> 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 purpo= se > >> 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. > > I think the question is: how else would you implement an interface that > doesn't restrict itself to returning a list? I mean, previously, the > following was totally inefficient in terms of memory: > > value in d.values() > > It now avoids creating an intermediate list copy of the values, thus > running with no additional memory overhead (well, a constant, ok, but > definitely not linear) and keeps users from resorting to the much more > unfriendly > > for v in d.itervalues(): > if v =3D=3D value: > return True > else: > return False > > in order to achieve the same thing. You can now even efficiently do this > for items, i.e. > > (key, value) in d.items() > > That's equivalent to "d[key] =3D=3D value", but uses a different protocol, > meaning that you don't have to make a copy of the dict items in order to > pass it into something that works on a set or iterable of 2-tuples (which > is a way more generic interface than requiring a dict as input). These > things chain much more cleanly now, without first having to explain the > difference between items() and iteritems() and when to use which. > > It's all about replacing the old copy-to-list interface by something that > is efficiently processable step by step. All of this started back when > iterators became a part of the language, then generators, and now dict > views. They may not be the hugest feature ever, but they definitely fit > into the language much better and much more cleanly than the old > copy-to-list way. > > Ask yourself, if they had been there in Python 1.x, would you even have > thought about making the iter*() methods a part of the language? Would you > really have wanted a shorter way to create a list of dict values than > list(d.values())? > > Stefan > I am still not clear on the advantage of views vs. iterators. What makes d.viewkeys() better than d.iterkeys()? Why did they decide not to rename d.iterkeys() to d.keys() and instead use d.viewkeys()? Is the iteration over a set operation on keys really that common a use case? Ramit This email is confidential and subject to important disclaimers and conditio= ns including on offers for the purchase or sale of securities, accuracy and= completeness of information, viruses, confidentiality, legal privilege, and= legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclo= sures/email.