3

Essentially, the problem was to create a function deepcopy(L) that will return a deep copy of a list L. However, we were told to not use the copy module or any function in it.

I'm a complete beginner in an intro class and am honestly struggling with this one. The only thing we were really told is that we should use recursion to solve the problem but after being stuck for so long I figured I'd ask for help.

Couldn't figure it out in class and didn't really get a clear answer and now the professor is moving on so ANY help would be greatly appreciated. Thanks.

asked Oct 11, 2017 at 23:01
8
  • 1
    We answer specific questions here. "ANY help would be greatly appreciated" isn't a question. You need to have a conversation with someone, and Stack Overflow isn't the place to do that. Commented Oct 11, 2017 at 23:10
  • 1
    If you want help with this you need to show us the code you've written so far and explain what it's doing wrong. Commented Oct 11, 2017 at 23:10
  • Sorry, this is the first time I've used the site. Truthfully, I don't even really know where to begin to make this work so I don't really have code written. Commented Oct 11, 2017 at 23:13
  • Do you have any experience writing recursive functions? Complete beginner aren't usually expected to write recursive code... This task isn't hard, but there are a few pitfalls. In particular, you need to be careful with strings. Commented Oct 11, 2017 at 23:17
  • Yeah, thats what everyone keeps telling me. The professor is purposely going much harder on us than you would expect in an intro class because he thinks this is the only way we'll learn anything. However it's like learning the spanish alphabet and then being told to go write an essay. Commented Oct 11, 2017 at 23:19

5 Answers 5

4

Maybe, you are looking for something like this:

def mydeepcopy(L):
 if isinstance(L, list):
 ret = []
 for i in L:
 ret.append(mydeepcopy(i))
 elif isinstance(L, (int, float, type(None), str, bool)):
 ret = L
 else:
 raise ValueError("Unexpected type for mydeepcopy function")
 return ret
lst = [ [1,2,3], [4,5,6], [7,8,9] ]
copy = mydeepcopy(lst)
print("Original Data: %s" % (lst))
print("Original IDs: %s = %s" % (id(lst),[ id(i) for i in lst ]))
print("Copied Data: %s" % (copy))
print("Copied IDs: %s = %s" % (id(copy),[ id(i) for i in copy ]))

Possible Output:

Original Data: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Original IDs: 140637274065288 = [140637274064712, 140637274064776, 140637274065352]
Copied Data: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Copied IDs: 140637274065480 = [140637274065416, 140637274065160, 140637274065096]
answered Oct 22, 2017 at 12:03
Sign up to request clarification or add additional context in comments.

Comments

2

Deepcopy can be done using this one line method,

def deep_copy(source_list):
 dest_list = source_list[:]
 return dest_list

It gives you the smaller function and reduce the processing time.

answered Oct 26, 2020 at 5:11

Comments

2

I would like to pinpoint the fact that performing

dest_list = source_list[:]

does NOT create a deepcopy.

To prove this try performing the following operations:

>>> list1 = [[1,2,3], [4,5,6], [7,8,9]]
>>> list2 = list1[:]
>>> id(list1) == id(list2)
False
>>> id(list1[0]) == id(list2[0])
True

As the nested objects are just a copy of the reference, this operation classifies as shallowcopy

answered Feb 15, 2022 at 9:03

Comments

0

It is a single line answer:

newList = [L[i] for i in range(len(L))]
answered Jun 10, 2020 at 14:42

Comments

0

The answer above by Aaron is correct, except that each list in a list comprehension also needs to be a copy. So,

newList = [L[i].copy() for i in range(len(L))]
answered Jan 28, 2024 at 22:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.