Shallow and deep copies Flashcards
Question: Consider a tuple as shown below:
Code Editor:
tuple_a = (“John”, “Amy”, “Jules”, [“Jim”, “Ana”])
What code would you run to create a deep copy of this tuple?
deep_copy = copy.deepcopy(tuple_a)
Question: Consider the following code:
Code Editor:
old_str = “Hello”
new_str = old_str
new_str = “World”
What will be the value stored in old_str after these lines of code are executed?
“Hello”
old_list = [“John”, “Amy”, “Jules”]
new_list = old_list.copy()
new_list.append(“Emily”)
What will be the value stored in old_list after these lines of code are executed?
[“John”, “Amy”, “Jules”]
Code Editor:
dict_a = {“John”: 35, “Jim”: 22, “Jill”: 44}
dict_copy = dict_a
dict_copy[‘John’] = 40
del dict_copy[‘John’]
After executing this code, what is the value associated with John in dict_a?
The key John does not exist in dict_a
For a set in Python what is the difference between copy.copy() and the copy.deepcopy() method?
No difference between the two because sets cannot hold mutable types like lists
old_list = [“John”, “Amy”, “Jules”]
new_list = old_list
new_list.append(“Emily”)
What will be the value stored in old_list after these lines of code are executed?
[“John”, “Amy”, “Jules”, “Emily”]
Code Editor:
dict_a = {“John”: 35, “Jim”: 22, “Jill”: 44}
dict_copy = dict_a.copy()
dict_copy[‘John’] = 40
After executing this code, what is the value associated with John in dict_a?
35