Unexpected behavior when assigning values to dictionary entries

Viewed 12

Please, can somebody explain why is the behavior in the example provided and what is the best way to avoid it:

If I try this it works as expected:

var1 = 'original'
var2 = var1

var2 = 'changed'

print(var1)

Out ---> "original"

BUT if I try the same on a dictionary:

var1 = {'data': 'original'}
var2 = var1

var2['data'] = 'changed'

print(var1)

Out ---> {'data': 'changed'}

Why does changing the value of var2 modifies the value of var1 when using dictionaries, but not otherwise?

Thanks

0 Answers
Related