[Baypiggies] processing multiple lists in step

wesley chun wescpy at gmail.com
Fri Sep 11 05:51:44 CEST 2009


On Thu, Sep 10, 2009 at 12:08 AM, Jeff
Enderwick<jeff.enderwick at gmail.com> wrote:
> I like using list-comps in Python. Is there a nice way to process more
> than one list concurrently in this style? By this, I mean that the 1st
> iteration would act on L1[0] and L2[0], the 2nd iteration would act on
> L1[1] and L2[1], and so on. Sorry if the soln is obvious - I am tired
> :-)


another recommendation is that if you're creating such data structures
where the only intent is to iterate over them and throw them away, i'd
like to try and convince you to use generator expressions instead of
list comprehensions. the syntax is nearly identical... just swap your
square brackets for parentheses.

listcomps create the entire list in memory before you get it back vs.
a genexp which returns only a generator object (it's really just an
iterator). the memory savings will eventually have a(n adverse)
performance impact vs. using genexps or any iterator-like object.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Baypiggies mailing list