question on list comprehensions

Carlos Ribeiro carribeiro at
Thu Oct 14 18:59:41 CEST 2004

On Thu, 14 Oct 2004 11:16:12 -0400, Darren Dale <dd55 at> wrote:
> I am simulating diffraction from an array of particles. I have to calculate
> a complex array for each particle, add up all these arrays, and square the
> magnitude of the result. If I do a for loop, it takes about 6-8 seconds for
> a 2000 element array added up over 250 particles. In reality, I will have
> 2500 particles, or even 2500x2500 particles.

Can't you just sum them inplace as you calculate every new array, for
each particle? Something like this (pseudo code):

result = make_empty_array()
for particle in particles:
    result += make_complex_array(particle)
return square(result)

It does not grow the result array indefinitely.... so it solves your
problem, if that's what you need/want to avoid.

Carlos Ribeiro
Consultoria em Projetos
mail: carribeiro at
mail: carribeiro at

More information about the Python-list mailing list