0

I'm trying to write a function that takes a list as an input and returns a modified version of that list such that the function does not change the input list. The input variable T is a list of lists. I have tried

def function(T):
  Tree = copy.copy(T)
  Do stuff with Tree
  return Tree

only to discover that T has been changed by the function. I have also tried Tree = T[:] but that also doesn't work. Any suggestions?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Likely the issue is you require a *deep* copy of the items in T (presumably, some sort of Node object). When you use a shallow copy, i.e. `copy.copy` or `my_list.copy()` or `my_list[:]` then the *list* is copied but the items inside the list are not. – juanpa.arrivillaga Jul 30 '18 at 00:18
  • This worked perfectly. THANK YOU!!! – A to the B Jul 30 '18 at 00:22

0 Answers0