[BangPypers] nested list question

Anand Balachandran Pillai abpillai at gmail.com
Thu Jul 22 13:41:56 CEST 2010


On Thu, Jul 15, 2010 at 9:50 AM, Srinivas Reddy Thatiparthy <
srinivas_thatiparthy at akebonosoft.com> wrote:

> I read from Python 2.6.3 docs that,copied from chm....
>
> " Note that filter(function, iterable) is equivalent to [item for item in
> iterable if function(item)] if function is not None and [item for item in
> iterable if item] if function is None."
>
> Since they said *equivalent* ,i thought both transform to same byte code.I
> missed the point that they are equal *logically*.
>

Never - "filter", "map" etc are implemented as functions so each
call involve a function call in Python which is costly. List comprehensions
are implemented in the language and hence much more optimized.

So if you are aiming to extract maximum juice from your Python
code, prefer native language constructs like comprehensions
as opposed to "outsiders" like map, filter etc. If required, always
prefer the itertools version of these functions such as itertools.imap
etc.


>
>
>
> Regards,
> Srini T.
> --
>
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>


-- 
--Anand


More information about the BangPypers mailing list