0

This questions is not a duplicate of how to copy a list, because it is essential that I keep my "list" variable the same. I cannot do

new_list = old.list.copy()

I am creating a game and the "list" is an ever-present variable. I am attempting to create actions that will essentially clone all of the contents of the EXISTING list while keeping the variable the same for future functions. Otherwise, if a function has:

list.append('Warrior')

It will only add the 'Warrior' to the old "list" and not the new_list that was created.

What I am attempting is simple. I have a list, and I want the contents to double, but in such a way that if I alter some contents of the list, I don't want it to do all of it.

list = [[x, y]]

I want

list=[[x, y], [x, y]]

But in such a way that I did

list[0][1] += 1

It would adjust the first x, and not the second (and doing it this way would alter both).

I've tried doing

list = [[x, y]]
list.append(copy.deepcopy(list))
print (list)
[[x, y], [[x, y]]]

Which doesn't work. What would you advise?

martineau
  • 119,623
  • 25
  • 170
  • 301
David William
  • 65
  • 2
  • 9
  • I would appreciate if you took another look at my post. The question you referred me to does not address my issue, since it does not clone the contents within the same list, it creates a different object which is not my objective. Thank you. – David William Oct 14 '18 at 13:48
  • What do you mean by clone the contents within the same list? – dangee1705 Oct 14 '18 at 14:38
  • Instead of having a separate and distinct list created that's a clone, cloning the contents of the list WITHIN the list, so instead of the list being list = [[x, y]], it becomes list = [[x, y], [x, y]] – David William Oct 14 '18 at 16:58
  • I'm sorry I don't see what the issue is here. Just append the first item on to the list – dangee1705 Oct 14 '18 at 17:01

0 Answers0