Copy the value of a variable type list, to another in python

0

I am declaring an X variable with the value of another Y using = , but when having a append in the variable X >, Y also changes.

Code:

'''
Resultado obtido:
['casaRJ', 'casaSP', 'casaDF', 'apRJ', 'apSP']                                                                                
['casaRJ', 'casaSP', 'casaDF', 'apRJ', 'apSP']

Resultado esperado:
['casaRJ', 'casaSP', 'casaDF']                                                                                
['casaRJ', 'casaSP', 'casaDF', 'apRJ', 'apSP']

'''

variavel_casas = ['casaRJ', 'casaSP', 'casaDF']
variavel_outros = ['apRJ', 'apSP', 'apDF']

principal = variavel_casas
suporte = variavel_casas

if 'casaRJ' in str(variavel_casas): suporte.append('apRJ')
if 'casaSP' in str(variavel_casas): suporte.append('apSP')

print(principal)
print(suporte)

How could I solve this? And why does this occur? Where is the problem in the code, and in what ways could I do this correctly?

    
asked by anonymous 30.12.2018 / 21:20

2 answers

2

In python when it comes to container variables (lists, dictionaries, etc) the idea is slightly different from other languages, when you do var1 = [value1, value2..] , var1 works as a kind of "alias" for the address (in memory) of the list, then if you do: var2 = var1 , you are just creating a new label for the list address, see this example:

casas1 = ['casa1', 'casa2', 'casa3']
casas2 = casas1
casas2[0] = 'casa99'

print(casas1)
['casa99', 'casa2', 'casa3']

See id s of variables:

id(casas1)
140096910106120

id(casas2)
140096910106120

To solve the problem you need to use the copy() method, see the same example, now with copy() :

casas1 = ['casa1', 'casa2', 'casa3']
casas2 = casas1.copy()
casas2[0] = 'casa99'

print(casas1)
['casa1', 'casa2', 'casa3']

Notice that% s of% s are now different:

id(casas1)
140096885079112

id(casas2)
140097875390664

If you want to copy the variables, in the lines where you do:

principal = variavel_casas
suporte = variavel_casas

Should do:

principal = variavel_casas.copy()
suporte = variavel_casas.copy()
    
30.12.2018 / 22:02
2

After testing and searching, I found an answer.

We could say that the variables that receive lists in Python end up being (implicitly) like the pointers of the C language. I admit that I had no idea that this was happening in Python, but, I became aware of it from answer given in this site , of which I transcribed a passage below:

  

If I'm not mistaken, there is no explicit concept of pointers in   Python, just like in C for example. The Python language treats some   types of variables as 'pointers' implicitly.

     

That is, primitive types (eg int, float) are passed by value   (or rather passes a copy of the variable). Already for other types (e.g.   list, dict) Python uses the address of the object [...]

Based on this, I think you can use the built-in class list to create a "copy" of the list; and thus, there will be no change to the base list.

variavel_casas = ['casaRJ', 'casaSP', 'casaDF']
variavel_outros = ['apRJ', 'apSP', 'apDF']

principal = list(variavel_casas)
suporte = list(variavel_casas)

if 'casaRJ' in variavel_casas: suporte.append('apRJ')
if 'casaSP' in variavel_casas: suporte.append('apSP')

print(principal)
print(suporte)

I hope I have helped!

    
30.12.2018 / 22:01