# Re: Numpy Array of Sets

```LJ wrote:

>
> Following the example in the link, the problem appears:
>
>>>> A = [[0]*2]*3```
```
You can see this as a shortcut for

value = 0
inner = [value, value]
A = [inner, inner, inner]

When the value is mutable (like your original set) a modification of the
value shows in all six entries. Likewise if you change the `inner` list the
modification shows in all three rows.

>>>> A
> [[0, 0], [0, 0], [0, 0]]
>>>> A[0][0] = 5
>>>> A
> [[5, 0], [5, 0], [5, 0]]
>
> Now, if I use a numpy array:
>
>>>> d=array([[0]*2]*3)
>>>> d
> array([[0, 0],
>        [0, 0],
>        [0, 0]])
>>>> d[0][0]=5
>>>> d
> array([[5, 0],
>        [0, 0],
>        [0, 0]])
>
>
> What is the difference here?

Basically a numpy array doesn't reference the lists, it uses them to
determine the required shape of the array. A simplified implementation might
be

class Array:
def __init__(self, data):
self.shape = (len(data), len(data[0]))
self._data = []
for row in data: self._data.extend(row)
def __getitem__(self, index):
y, x = index
return self._data[y * self.shape[1] + x]

With that approach you may only see simultaneous changes of multiple entries
when using mutable values.

--
https://mail.python.org/mailman/listinfo/python-list
```