In the Python documentation it is explained that s * n
or n * s
means items in the sequence s are not copied; they are referenced multiple times. I am able to get that theoretically but unable to understand the example they have given:
>>> lists = [[]] * 3
>>> lists
[[], [], []] # Output
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]] # Output
I am confused because if we do the similar thing with strings, it behaves differently as:
list_of_str = (["str"] * 3)
print(list_of_str)
list_of_str[0] = "str_1"
print(list_of_str)