Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.06; 'intermediate': 0.07; 'list?': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'subject:()': 0.09; '~ethan~': 0.09; 'language,': 0.12; '(key,': 0.16; '12:59': 0.16; 'cleanly': 0.16; 'dict': 0.16; 'iterable': 0.16; 'iterators': 0.16; 'mean,': 0.16; 'previously,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'replacing': 0.19; 'stefan': 0.19; 'fit': 0.20; 'memory': 0.22; 'creating': 0.23; 'header:User- Agent:1': 0.23; 'question': 0.24; 'equivalent': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'feature': 0.29; 'thus': 0.29; "doesn't": 0.30; 'restrict': 0.30; '(which': 0.31; 'overhead': 0.31; 'interface': 0.32; 'running': 0.33; 'totally': 0.33; 'something': 0.35; 'but': 0.35; 'false': 0.36; 'i.e.': 0.36; 'returning': 0.36; 'charset:us-ascii': 0.36; 'list': 0.37; 'step': 0.37; 'implement': 0.38; 'thank': 0.38; 'generic': 0.38; 'requiring': 0.38; 'to:addr:python-list': 0.38; 'pm,': 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; 'received:173': 0.61; 'first': 0.61; 'you.': 0.62; 'back': 0.62; 'became': 0.64; 'more': 0.64; 'different': 0.65; 'between': 0.67; 'subject: & ': 0.68; 'avoids': 0.84; 'protocol,': 0.84; 'inefficient': 0.91; 'items,': 0.91; 'step.': 0.91 Date: Wed, 24 Jul 2013 13:16:54 -0700 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Python 3: dict & dict.keys() References: <51EF2AD8.3080105@stoneleaf.us> <51F01D9E.2030903@stoneleaf.us> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.233]) [173.12.184.233]:45868 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374698328 news.xs4all.nl 15971 [2001:888:2000:d::a6]:43149 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51165 On 07/24/2013 12:59 PM, Stefan Behnel wrote: > > 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 == 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] == 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. Thank you. :) -- ~Ethan~