[Python] How do I clone a list so that it doesn't change unexpectedly after assignment?

Discussão em 'Python' iniciado por Stack, Janeiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    While using new_list = my_list, any modifications to new_list changes my_list every time. Why is this, and how can I clone or copy the list to prevent it? For example:

    >>> my_list = [1, 2, 3]
    >>> new_list = my_list
    >>> new_list.append(4)
    >>> my_list
    [1, 2, 3, 4]

    Continue reading...

Compartilhe esta Página