## Initialising lists of arrays in Python

### January 23, 2009

I often work with lists of arrays or matrices. For example if I have a state sequence $X = {x_1, x_2 \ldots x_T}$, it’s often useful to store each state as a numpy.matrix so I can do fun things like write

```for x in X:
outer_product = x * x.T
```

which pleases me. Sometimes I need to initialise these lists and I keep tripping up on the same problem. I can think of two ways to initialise a list of arrays:

```[pylab.empty(n,m)] * T
[pylab.empty(n,m) for t in range(T)]
```

Way number one is BAD and doesn’t work quite how I’d expect. What happens is that the array is created and then repeated T times. This is bad because each element of the resulting list is simply a pointer at a single array. So whenever you change any element of the list it changes EVERY element of the list. Gah!

Way number two is better! Here the list comprehension makes T empty matrices, which you can then edit to your heart’s content. Hurrah!

### 3 Responses to “Initialising lists of arrays in Python”

1. jameshensman Says:

Hey up.

could you explain what the empty() command produces, please?

Whats wrong with [np.zeros((n,m)) for t in range(T)]?

James.

2. mikedewar Says:

empty() makes an array with seemingly crazy numbers in it. Apparently it’s “marginally” faster than zeros (see http://docs.scipy.org/doc/numpy/reference/generated/numpy.empty.html ) but what I like about it is that when I forget to fill it up, the crazy values are likely to cause much more problems than some happy quiet little zeros.

In all honesty, though, I doubt it makes much difference!

3. jameshensman Says:

Ah, makes sense. I like the mentality of “I’m going to make this fragile so that it will either work fine or explode like a fistful of lithium”. Nice 🙂