# How to properly copy a list of dictionaries in python?

**URL:** <https://forum.inductiveautomation.com/t/how-to-properly-copy-a-list-of-dictionaries-in-python/54522>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [December 29, 2021, 6:18pm UTC](https://forum.inductiveautomation.com/t/how-to-properly-copy-a-list-of-dictionaries-in-python/54522 "2021-12-29T18:18:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [December 29, 2021, 6:18pm UTC](https://forum.inductiveautomation.com/t/how-to-properly-copy-a-list-of-dictionaries-in-python/54522/1 "2021-12-29T18:18:54Z")

</div>

I’ve been having trouble with a script and I isolated the issue to the fact that my for loops are somehow modifying an unrelated list (as far as I can tell). I replicated the result with this piece of code:

```python
list1 = [{"key1": 1, "key2": 2}]

list2 = list(list1)

print "ID of list1: " + str(id(list1))
print "ID of list2: " + str(id(list2))
print "Contents of list1: " + str(list1)
print "Contents of list2: " + str(list2)

list2[0]["key1"] = 999
		
print "ID of list1: " + str(id(list1))
print "ID of list2: " + str(id(list2))
print "Contents of list1: " + str(list1)
print "Contents of list2: " + str(list2)

```

It returns this:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/5/c5e6d1b3d277a58e2df26b0fb92fe14d1e96ad6f.png)

This makes absolutely no sense to me, because the IDs of the lists are different, but the code modifying list2 somehow also modifies list1…

This piece of code works as expected:

```python
list1 = [1, 2]
list2 = list(list1)
list2[0] = 999
print list1
print list2

```

returning:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/7/b712977fd16daea1d139de8c2e614c468b1c23c9.png)

Does this have something to do with dictionaries? I am very confused…

If it is the dictionaries, is there a proper way to copy a list of dictionaries to actually create two different lists?

I would appreciate some insight.

---

<div class="post-metadata">

**Author:** ![YF129701](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/yf129701/32/60498_2.png) [@YF129701](https://forum.inductiveautomation.com/u/YF129701)\
**Post date:** [December 29, 2021, 6:27pm UTC](https://forum.inductiveautomation.com/t/how-to-properly-copy-a-list-of-dictionaries-in-python/54522/2 "2021-12-29T18:27:59Z")

</div>

I think it has to do with deep vs shallow copying. I only know this because I ran into a very similar situation earlier this year.

When you say `list2 = list1`, `list2` is a reference to `list1`, so any changes you make to `list1` will also modify `list2`.

```python
import copy
list2 = copy.deepcopy(list1)

```

Try this out.

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [December 29, 2021, 6:32pm UTC](https://forum.inductiveautomation.com/t/how-to-properly-copy-a-list-of-dictionaries-in-python/54522/3 "2021-12-29T18:32:31Z")

</div>

Thanks for the reply. That’s what I though at first as well, but I’m not simply doing list2 = list1. But rather list2 = list(list1) which doesn’t copy the reference, but actually makes another list.  
The IDs of both lists are different in the above example, so the code should be referencing different lists.

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [December 29, 2021, 6:34pm UTC](https://forum.inductiveautomation.com/t/how-to-properly-copy-a-list-of-dictionaries-in-python/54522/4 "2021-12-29T18:34:35Z")

</div>

I tried copy.deepcopy() and it worked. Thank You!

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [January 5, 2022, 9:42am UTC](https://forum.inductiveautomation.com/t/how-to-properly-copy-a-list-of-dictionaries-in-python/54522/5 "2022-01-05T09:42:59Z")

</div>

Actually, it makes a different list containing different references to the same elements.  
The key thing here is that your list contains references, not values. When copying the list, even if it’s a brand new one, the things inside still reference the same things.

Imagine copying a list of pointers. You end up with 2 different lists, that contain different pointers, but that point to the same place. Now, if you tried `list2[0] = '999'`, that won’t change `list1[0]`, only assign 999 to `list2[0]`.  
BUT, `list2[0][0] = 999` WILL modify the thing that was pointed at by both `list1[0]` and `list2[0]`.

If you look back at your first code snippet, you’ll see you’re using `list2[0]['key1'] = 999`. You’re not modifying the first element of list2, you’re modifying the object referenced by the first element of list2. Which happens to also be referenced by the first element of list1.

---

<div class="post-metadata">

**Author:** ![djs](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@djs](https://forum.inductiveautomation.com/u/djs)\
**Post date:** [January 5, 2022, 1:14pm UTC](https://forum.inductiveautomation.com/t/how-to-properly-copy-a-list-of-dictionaries-in-python/54522/6 "2022-01-05T13:14:36Z")

</div>

This makes sense. Thank You for the reply.
