[Numpy-discussion] a 'for' loop within another 'for' loop?

Warren Focke focke at slac.stanford.edu
Tue Jul 13 22:02:08 CDT 2004

l = Numeric.add.outer(r, r).flat
oughta do the trick.  Should work for numarray, too.

On Tue, 13 Jul 2004, Hee-Seng Kye wrote:

> Hi.  I wrote a program to calculate sums of every possible combinations
> of two indices of a list.  The main body of the program looks something
> like this:
> r = [0,2,5,6,8]
> l = []
> for x in range(0, len(r)):
>      for y in range(0, len(r)):
>          k = r[x]+r[y]
>          l.append(k)
> print l
> 1. I've heard that it's not a good idea to have a 'for' loop within
> another 'for' loop, and I was wondering if there is a more efficient
> way to do this.
> 2. Does anyone know if there is a built-in function or module that
> would do the above task in NumPy or Numarray (or even in Python)?
> I would really appreciate it if anyone could let me know.
> Thanks for your help!

More information about the Numpy-discussion mailing list