Ad
  • Default User Avatar

    Actually it duplicates the reference on an element.

    my_list = [[1]] * 3
    print(my_list)
    >>> [[1], [1], [1]]
    my_list[0].append(2)
    print(my_list)
    >>> [[1, 2], [1, 2], [1, 2]]
    

    But as you mentionned, there is no problem with primitive and immutable types: int, str, tuple, frozenset...

    my_list = [1] * 3
    print(my_list)
    >>> [[1], [1], [1]]
    my_list[0] += 1
    print(my_list)
    >>> [2, 1, 1]