Sunny finds out that if you run
a=[0,1,2]
b=[0,1,2]
a[1]=2
print(a,b)
and
a=[0,1,2]
b=a
a[1]=2
print(a,b)
It gets different results. Sunny prefers the first. The problem is that you can not often rewrite the lists, especially if it is a dynamic list. Sunny found an workaround:
a=[0,1,2]
b=a[::]
a[1]=2
print(a,b)
The [::]
operator (or the [:]
operator) splices the list, producing the list starting from index 0
and ending at index -1
, inclusive.
However, this does not work with embedded lists:
a=[[0]]
b=a[:]
a[0][0]=1
print(a,b)
So Sunny wants to use recursion.
GUANRANTEE: There will be no recursion errors.
So, how do you code it? (Write a function named copy
that takes in the list)
TEST DATA: test=[[1,2,3],1,[[[1]],2,3,[3,[4,5]]],1]
TEST CODE: print(copy(test)[2][3][1]is test[2][3][1],copy(test)[2][3][1]==test[2][3][1])
DESIRED OUTPUT: False True