Does the method (Copy ()) of the lists in python make sense? (Python)

3

The method copy() makes a superficial copy of the list, but I do not see any sense to use it, if I want to make a copy of the list I simply do:

lista2 = lista1

Instead of:

lista2 = lista1.copy()

What is the copy() method for?

    
asked by Power 17.03.2017 в 23:29
source

1 answer

4

When, you assign a list to another, what you do is pass a reference to the first list. instead, with a copy you create a new independent instance.

a = [1, 2, 3]
b = a
b[0] = 7
print(a[0])  # devuelve 7


a = [1, 2, 3]
b = a.copy()
b[0] = 7
print(a[0])  # devuelve 1
    
answered by 17.03.2017 / 23:43
source