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


Groups > comp.lang.python > #87668

Re: Dict comprehensions - improvement to docs?

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 <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.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" <frank@chagford.com>
Subject Re: Dict comprehensions - improvement to docs?
Date Wed, 18 Mar 2015 10:01:36 +0200
References <mailman.410.1426483550.21433.python-list@python.org> <87fv95fom0.fsf@jester.gateway.sonic.net> <mailman.427.1426494675.21433.python-list@python.org> <87wq2hfibu.fsf@jester.gateway.sonic.net> <me664q$hon$1@ger.gmane.org> <CALwzid=u19YMkfJBhbLZi1qh2U9UK4ohY5wco1zO-i3T5AtyOA@mail.gmail.com> <me8ige$kjp$1@ger.gmane.org> <CALwzidmNDcSvER7S6inEaVZA=DHUrDX1KzL-WRVwhd=o3_LvWA@mail.gmail.com>
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 <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.513.1426665702.21433.python-list@python.org> (permalink)
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

Show key headers only | View raw


"Ian Kelly" <ian.g.kelly@gmail.com> wrote in message 
news:CALwzidmNDcSvER7S6inEaVZA=DHUrDX1KzL-WRVwhd=o3_LvWA@mail.gmail.com...
> On Tue, Mar 17, 2015 at 12:44 AM, Frank Millman <frank@chagford.com> 
> 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


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


Thread

Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-16 07:25 +0200
  Re: Dict comprehensions - improvement to docs? Paul Rubin <no.email@nospam.invalid> - 2015-03-15 23:22 -0700
    Re: Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-16 10:30 +0200
      Re: Dict comprehensions - improvement to docs? Paul Rubin <no.email@nospam.invalid> - 2015-03-16 01:38 -0700
        Re: Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-16 11:01 +0200
        Re: Dict comprehensions - improvement to docs? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-16 09:57 -0600
          Re: Dict comprehensions - improvement to docs? Paul Rubin <no.email@nospam.invalid> - 2015-03-16 15:41 -0700
        Re: Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-17 08:44 +0200
        Re: Dict comprehensions - improvement to docs? Lele Gaifax <lele@metapensiero.it> - 2015-03-17 15:06 +0100
        Re: Dict comprehensions - improvement to docs? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-17 09:48 -0600
        Re: Dict comprehensions - improvement to docs? "Frank Millman" <frank@chagford.com> - 2015-03-18 10:01 +0200
        Re: Dict comprehensions - improvement to docs? Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-18 02:39 -0600

csiph-web