Python, clone the lists

Asked

Viewed 4,065 times

5

Be the code below:

def modif1(lista):
    lista = [4,5,6]
lista = [1,2,3]
modif1(lista)
print(lista) #resultado: [1,2,3]


def modif2(lista):
    lista[0] = 4
    lista[1] = 5
    lista[2] = 6
lista = [1,2,3]
modif2(lista)
print(lista) #resultado: [4,5,6]

def modif3(lista):
    lista[:] = [4,5,6]
lista = [1,2,3]
modif3(lista)
print(lista) #resultado: [4,5,6]

def modif4(lista):
    L = lista[:]
    L = [4,5,6]
lista = [1,2,3]
modif4(lista)
print(lista) #resultado: [1,2,3]

The function modif1 does not change the list because the function’s Scope already has a variable with the list name. The function modif2 modifies the list because it has no name variable list and accesses list (in global Scope). In function 3 comes the unexpected: when do lista[:] I’m not making a clone of lista? Why then when modifying lista[:] i modify the lista original and not just the clone? If so, what changes modif4 of modif3?

1 answer

8


In Python, everything is object. And parameters are passed by assignment (which more or less means that references to objects are passed by value). So if you make a change to the variable of the lista it does not stay outside the scope. Just like in your code, it also occurs in this simpler example:

def teste(a, b):
    a = 10
    b = 'Hello World!'

x = 1
y = 'Bla ble bli blo blu'
teste(x, y)
print(x, y) # Imprime: 1 Bla ble bli blo blu

It turns out that some objects are mutable, as is the case with lists. So when you do lista[0], you are changing an object element (whose reference you received as parameter), and so this change persists. The same goes for lista[:]. In this case you are changing several elements of the object instead of one. When you do lista = <qualquer coisa>, you threw away your original object and created a new, local scope.

If you really want to make a copy of a list, use lista.copy:

def teste(lista):
    lista[0] = 99

lista = [i for i in range(10)]
teste(lista)
print(lista) # Imprime: 99, 1, 2, 3, 4, 5, 6, 7, 8, 9]

lista = [i for i in range(10)]
teste(lista.copy())
print(lista) # Imprime: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.