2
I’m declaring a variable X with the value of another Y using =
, but when having do a append
in the variable X, to 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 is this happening? Where is the problem in the code, and in what ways could I do it properly?
Why are you converting the list to string, if the test with
in
also works with lists?– Gustavo Sampaio
I didn’t know, I’m still learning these details. Thanks for the tip @Gustavosampaio
– Florida