Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed3.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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '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; 'comp': 0.16; 'dict': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'bit': 0.19; 'machine': 0.22; 'load': 0.23; 'header:X-Complaints-To:1': 0.27; '100000': 0.31; 'writes:': 0.31; 'but': 0.35; 'much.': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'frank': 0.68; 'skip:n 40': 0.81 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Dict comprehensions - improvement to docs? Date: Mon, 16 Mar 2015 11:01:22 +0200 References: <87fv95fom0.fsf@jester.gateway.sonic.net> <87wq2hfibu.fsf@jester.gateway.sonic.net> X-Gmane-NNTP-Posting-Host: 197.89.15.108 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426496495 news.xs4all.nl 2840 [2001:888:2000:d::a6]:36634 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87539 "Paul Rubin" wrote in message news:87wq2hfibu.fsf@jester.gateway.sonic.net... > "Frank Millman" writes: >> dict((a, b) for a, b in zip(x, y)) >> 100000 loops, best of 3: 16.1 usec per loop >> {a: b for a, b in zip(x, y)}" >> 100000 loops, best of 3: 6.38 usec per loop > > Hmm, I bet the difference is from the (a,b) consing all those tuples. > > Can you try just dict(zip(x,y)) ? C:\>python -m timeit -s "x = range(65, 91); y = (chr(z) for z in x)" "dict(zip(x, y))" 100000 loops, best of 3: 11.9 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)}" 100000 loops, best of 3: 7.24 usec per loop I ran the dict comp again in case the machine load had changed - it is a bit slower, but not much. Frank