Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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.028 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'run-time': 0.05; 'constructor': 0.09; 'executed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:c 10': 0.09; 'wrote': 0.14; 'dict': 0.16; 'expression.': 0.16; 'no-op': 0.16; 'questioned': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'stumbled': 0.16; 'two,': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'pointed': 0.19; 'tests': 0.22; 'comparing': 0.24; 'paul': 0.24; 'compare': 0.26; 'post': 0.26; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'am,': 0.29; 'statement': 0.30; "skip:' 10": 0.31; 'explained': 0.31; 'go.': 0.31; 'another': 0.32; 'quite': 0.32; 'not.': 0.33; 'agree': 0.35; 'but': 0.35; 'revert': 0.36; 'thanks': 0.36; "i'll": 0.36; 'possible': 0.36; 'subject:?': 0.36; 'example,': 0.37; 'easily': 0.37; 'to:addr:python-list': 0.38; 'fact': 0.38; 'previous': 0.38; 'anything': 0.39; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'expression': 0.60; 'introduced': 0.61; 'entire': 0.61; 'first': 0.61; 'back': 0.62; 'such': 0.63; 'more': 0.64; 'mar': 0.68; '10000': 0.68; 'frank': 0.68; 'statement,': 0.68; 'special': 0.74; '2015': 0.84; 'dict()': 0.84; 'difference.': 0.84; 'execution,': 0.91; 'mistake': 0.91; 'whereas': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Dict comprehensions - improvement to docs? Date: Wed, 18 Mar 2015 10:01:36 +0200 References: <87fv95fom0.fsf@jester.gateway.sonic.net> <87wq2hfibu.fsf@jester.gateway.sonic.net> X-Gmane-NNTP-Posting-Host: 197.86.222.101 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426665702 news.xs4all.nl 2968 [2001:888:2000:d::a6]:57206 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87668 "Ian Kelly" wrote in message news:CALwzidmNDcSvER7S6inEaVZA=DHUrDX1KzL-WRVwhd=o3_LvWA@mail.gmail.com... > On Tue, Mar 17, 2015 at 12:44 AM, Frank Millman > wrote: >> Thanks for the explanation. I'll try not to make that mistake again. >> >> However, to go back to the original example, we want to compare a dict >> comprehension with a dict() constructor using a generator expression. >> >> Let's see if I have got this one right - >> >> C:\>python -m timeit -s "x=range(65, 91); y=[chr(z) for z in x]" >> "dict((a, >> b) for a, b in zip(x, y))" >> 10000 loops, best of 3: 49.6 usec per loop >> >> C:\>python -m timeit -s "x=range(65, 91); y=[chr(z) for z in x]" "{a: b >> for >> a, b in zip(x, y)}" >> 10000 loops, best of 3: 25.8 usec per loop > > Why did you revert back to the no-op generator expression in the first > case instead of the more efficient dict(zip(x, y))? Firstly, I just want to emphasise that I am not trying to prove anything here, I am trying to learn something. Why do you call it a no-op? I understood your previous point that a generator in the setup statement is exhausted after the first execution, but this is in the run-time statement, so I thought it would be executed every time. If one does not need any 'comprehension' functionality, I agree that dict(zip(x, y)) is the way to go. However, if you do need a comprehension, Paul questioned why a special dict comprehension was introduced when you can easily use a generator expression. I ran the timing tests to compare the two, and I was surprised to see such a difference. However, you have already pointed out one case where I was not comparing apples and apples, and it is quite possible that I have stumbled across another one. I read Lele's post where he pointed out that part of the difference is explained by the fact that dict() involves a function call, whereas {} does not. However, this does not seem to explain the entire difference. Frank