[BangPypers] Are comprehensions faster?

Chetan Nichkawde chetan.nichkawde at gmail.com
Mon Feb 9 13:25:56 CET 2009


Are comprehensions (list, dict, set) supposed to run faster than regular
approach?

On Mon, Feb 9, 2009 at 4:30 PM, <bangpypers-request at python.org> wrote:

> Send BangPypers mailing list submissions to
>        bangpypers at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>        http://mail.python.org/mailman/listinfo/bangpypers
> or, via email, send a message with subject or body 'help' to
>        bangpypers-request at python.org
>
> You can reach the person managing the list at
>        bangpypers-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of BangPypers digest..."
>
>
> Today's Topics:
>
>   1. Comprehending the comprehensions (Anand Balachandran Pillai)
>   2. Looking for python Job (M Kumar)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 9 Feb 2009 12:09:46 +0530
> From: Anand Balachandran Pillai <abpillai at gmail.com>
> Subject: [BangPypers] Comprehending the comprehensions
> To: Bangalore Python Users Group - India <bangpypers at python.org>
> Message-ID:
>        <8548c5f30902082239y2b0de5a6nc871e9756b5958dd at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> Python 3.0 introduces two new ways of "comprehending".
>
> We had list comprehensions before. Now there are set &
> dict comprehensions in the language.
>
> Let us say there are two lists. For generating the list of unique
> products of numbers in the list, in Python 2.x, this could be
> done in 2 approaches.
>
> 1. Using a set,
>
> >>> l=[1,2,3,4]
> >>> m=[3,4,5,6]
> >>> set([x*y for x in l for y in m])
> {3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24}
>
> 2. Using a dictionary and inserting the values as keys,
>
> >>> l=[1,2,3,4]
> >>> m=[3,4,5,6]
> >>> d = {}
> >>> [d.setdefault(x*y, 0) for x in l for y in m]
> >>> d.keys()
> [3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
> >>>
>
> In Python 3.0, this is straight-forward using "set comprehensions".
> Set comprehensions are like list comprehensions but with braces replacing
> the square brackets. And they produce sets, not lists.
>
> So the approach (1) in py3k is a one-liner.
>
> >>> {x*y for x in l for y in m}
> {3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24}
>
> The approach (2) is also possible, since we now have "dict comprehensions"
> too! Which means you can build a dict in place without having to define one
> before and using 'setdefault' etc.
>
> >>> d={x*y:0 for x in l for y in m}
> >>> list(d.keys())
> [3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24]
>
> Note that the (list(d.keys)) is needed in Py3k since d.keys() no longer
> return a list as it used to in Py 2.x, but instead returns a generator.
>
> Regards,
>
> --Anand
>
>
>
>
> --
> -Anand
>
>
> ------------------------------
>
> Message: 2
> Date: Mon, 9 Feb 2009 16:25:39 +0530
> From: M Kumar <tomanishkb at gmail.com>
> Subject: [BangPypers] Looking for python Job
> To: bangpypers at python.org
> Message-ID:
>        <783b47270902090255t9b10a2dx695f578ed039e006 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hi all
>
> I am looking for python openings, I have 2 years experience in python
> programming. If anyone come to know about any suitable openings kindly let
> me know
>
> --
> Regards,
>
> Maneesh KB
>
> Comat Technologies
>
> Bangalore
>
> Mob: 9740-192309
>
>
>
> We work with the underprivileged and in rural India. If you are interested
> to be a part of it, please mail or call me. I will be happy to share and
> inform - http://www.comat.com
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/bangpypers/attachments/20090209/37f6f2e1/attachment-0001.htm
> >
>
> ------------------------------
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>
> End of BangPypers Digest, Vol 18, Issue 7
> *****************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20090209/9d9152f1/attachment.htm>


More information about the BangPypers mailing list