populating a doubly-subscripted array

Gregory Ewing greg.ewing at canterbury.ac.nz
Tue Nov 9 16:09:52 EST 2010


Mark Wooding wrote:

> What [x] * n does is make a list, whose length is n, and all of whose
> entries are precisely the value x.  If x itself is a list, then you get
> an outer list all of whose entries are the /same/ inner list

A reasonably elegant way to fix this is to use list comprehensions
for all except the innermost list:

    ff = [[0.0]*5 for i in xrange(5)]

If you're computing heavily with arrays, you might also consider
using numpy arrays instead of lists.

-- 
Greg



More information about the Python-list mailing list