[Tutor] List comprehensions

linuxian iandsd pylinuxian at gmail.com
Wed Apr 9 22:01:35 CEST 2008


On Wed, Apr 9, 2008 at 7:44 PM, Jerry Hill <malaclypse2 at gmail.com> wrote:

> On Wed, Apr 9, 2008 at 7:12 AM, Dinesh B Vadhia
> <dineshbvadhia at hotmail.com> wrote:
> > I want to replace the for loop with a List Comrehension (or whatever) to
> > improve performance (as the data list will be >10,000].  At each stage
> of
> > the for loop I want to print the result ie.
>
> List comprehensions are for building lists, not consuming them.  If
> you want to do something with every element in a list, other than
> building a new list with it, you should be using a for loop.
>
> > [print (item + "\n")  for item in data]
> >
> > But, this doesn't work as the inclusion of the print causes an invalid
> > syntax error.
> >
> > Any thoughts?
>
> Don't do this.
>
> Perhaps you could share a small piece of code that you think is too
> slow, and ask for advice in speeding it up?  If you're not sure which
> small pieces of code are too slow, you need to profile your
> application to find out.  See the documentation for python's profile
> module.  If you don't have enough code written to profile, then it's
> probably too early to be doing these optimizations.
>
> --
> Jerry
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


if you explain the source of the list, what do you want to change in it,
what destination will it take, i m sure the guys here will help a lot.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20080409/e0f56428/attachment.htm 


More information about the Tutor mailing list