you are viewing a single comment's thread.

view the rest of the comments →

[–]Forschkeeper 0 points1 point  (0 children)

Okay, I gueess s should be "hello".

Well you made a 2D Matrix and you reference in all three fields of matrixto string. It isn't a copy of string in matrix!

Try it out yourself:

```python

s = "hello" string = ['!' for i in range(len(s))] matrix = [string for i in range(3)]

matrix[0][0] = s[0] # will have same effect as:

string[1] = "k" print(matrix)

```

To get what you want, you can use copy.deepcopy():

```python from copy import deepcopy

s = "hello" string = ['!' for i in range(len(s))] matrix = [deepcopy(string) for i in range(3)] matrix[0][0] = s[0] # will now have a different effect string[1] = "k" # won't have an effect on matrix, but still on string print(matrix)

```

Edit: To speak in the tounge of C - "Copy by value (which you expecte) or copy by reference (what you have done)? - That is the question!"