0

I want to add a list (a) to another list (b). But with the twist, that I can change list a and the added list a in list b stays as it was. So that I can add list a again.

for example:

a = [a, b , c]
b = [1, 2]
b.append(a)

now b is [1, 2, a, b, c]

a = [4, 3]

and now b should stay [1, 2, a, b, c] so that I could append b again by a so, that it would be [1, 2, a, b, c, 4, 3]

So I try to create a list b, that is like an overview of the whole thing and need the list a, that I added earlier to stay the same in list b while I change it in the second run through the for loop.

(I am still kinda new to python and stack overflow so sorry for any mistakes xd)

Edit: I saw, that this question got linked to similar one, but the key difference (at least in my eyes) is, that I don't want to mutplicate the same thing, but to add a list (a) to another list (b) and being able to change a. At the same though the a in b should stay the same.

eyllanesc
  • 190,383
  • 15
  • 87
  • 142
Erbond12
  • 19
  • 6
  • In response to your edit: I think it's still the same fundamental issue, since you are sharing the same `list` object. A good reference for this sort of thing is Ned Batchelder's classic essay [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html), which explains exactly what's going on here. – Daniel Pryden Feb 20 '19 at 03:12
  • But isn't there a method, that I could call with b to save the early version of a and not just refer to a? – Erbond12 Feb 20 '19 at 03:15
  • Are you looking for [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – Daniel Pryden Feb 20 '19 at 03:41
  • I think so. I'll look over it in a second and give more details. – Erbond12 Feb 20 '19 at 15:22
  • Yeah that was what I was looking for :) Thank you! – Erbond12 Feb 20 '19 at 15:51

0 Answers0