Taking coursea class and not sure I understand the following. In the code example below I don't understand why 'marsupials' is added to the variables 'original' and 'shallow_copy' however "Hi there" is only added to 'original'. I thought the shallow copy was more or less a pointer to the same spot in memory
Thanks for any help understanding this better
Thanks for any help understanding this better
import copy original = [['canines', ['dogs', 'puppies']], ['felines', ['cats', 'kittens']]] shallow_copy_version = original[:] deeply_copied_version = copy.deepcopy(original) original.append("Hi there") original[0].append(["marsupials"]) print("-------- Original -----------") print(original) print("-------- deep copy -----------") print(deeply_copied_version) print("-------- shallow copy -----------") print(shallow_copy_version)
Output:-------- Original -----------
[['canines', ['dogs', 'puppies'], ['marsupials']], ['felines', ['cats', 'kittens']], 'Hi there']
-------- deep copy -----------
[['canines', ['dogs', 'puppies']], ['felines', ['cats', 'kittens']]]
-------- shallow copy -----------
[['canines', ['dogs', 'puppies'], ['marsupials']], ['felines', ['cats', 'kittens']]]
Larz60+ write Dec-01-2022, 07:06 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.