0

I want to save a copy of my old list. But whatever I do it is changing. My old list output contains of dictionaries. output_work is a copy. Example of output before actions:

enter image description here

What I was doing:

output_work = output.copy()

and

output_work = [i for i in output]

and

output_work = list(output)

and

import copy
output_work = copy.copy(output)

and

output_work = []
output_work.extend(output)

After some actions with output_work the result is next

enter image description here

What can I do to keep original output?

martineau
  • 119,623
  • 25
  • 170
  • 301
Alona
  • 67
  • 1
  • 10
  • 1
    You need to use `copy.deepcopy()`. You're copying the list, but both copies have references to the same dictionaries. – Barmar Dec 03 '20 at 18:02
  • 1
    [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – martineau Dec 03 '20 at 18:04
  • Or, you can just "deepcopy" manually, in this case, `[d.copy() for d in output]`. Basically, you need to copy the dicts *inside* the list as well, otherwise `list.copy` doesn't do that (and equivalent methods). They are shallow copies – juanpa.arrivillaga Dec 03 '20 at 18:07

0 Answers0