# Item in nested dictionary in a list can't be modified

**URL:** <https://forum.inductiveautomation.com/t/item-in-nested-dictionary-in-a-list-cant-be-modified/73098>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [May 1, 2023, 5:29pm UTC](https://forum.inductiveautomation.com/t/item-in-nested-dictionary-in-a-list-cant-be-modified/73098 "2023-05-01T17:29:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlos.aguero](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@carlos.aguero](https://forum.inductiveautomation.com/u/carlos.aguero)\
**Post date:** [May 1, 2023, 5:29pm UTC](https://forum.inductiveautomation.com/t/item-in-nested-dictionary-in-a-list-cant-be-modified/73098/1 "2023-05-01T17:29:36Z")

</div>

good day  
When adding a dictionary array inside a list I am not able to modify one of the dictionary items, instead all the dictionary items are modified...

```auto

#create a dictionary
dcirc={'mode':1,'alarm':4,'step':7}

#append the same dictionary in a list for 3 times
lcirc=[]
for x in range(3):
	lcirc.append(dcirc) 

#modify item 'alarm' from list array 2 
lcirc[2]['alarm']=99

```

Result: (all the 'alarm' item changes)  
**[{'mode': 1, 'alarm': 99, 'step': 7}, {'mode': 1, 'alarm': 99, 'step': 7}, {'mode': 1, 'alarm': 99, 'step': 7}]**

* * *

The only way I can get it to work is If I create the dictionary directly in the append instruction (instead of using the dictionary variable).

```auto

#append the dictionary in a list
lcirc=[]
for x in range(3):
	lcirc.append({'mode':1,'alarm':4,'step':7}) #<--- NOT using dictionary variable

#modify item 'alarm' from list array 2 
lcirc[2]['alarm']=99

```

Result: \*\*It works  
**[{'mode': 1, 'alarm': 4, 'step': 7}, {'mode': 1, 'alarm': 4, 'step': 7}, {'mode': 1, 'alarm': 99, 'step': 7}]**

I am just wondering what I am doing wrong when using the dictionary variable that causes problems when trying to modify one item.. any clues?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 1, 2023, 5:32pm UTC](https://forum.inductiveautomation.com/t/item-in-nested-dictionary-in-a-list-cant-be-modified/73098/2 "2023-05-01T17:32:50Z")

</div>

You're adding the same dictionary instance to the list, 3 times. You're not adding copies of it.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 1, 2023, 5:33pm UTC](https://forum.inductiveautomation.com/t/item-in-nested-dictionary-in-a-list-cant-be-modified/73098/3 "2023-05-01T17:33:17Z")

</div>

> **[Programming FAQ](https://docs.python.org/3/faq/programming.html#why-did-changing-list-y-also-change-list-x)**
>
> Contents: Programming FAQ- General Questions- Is there a source code level debugger with breakpoints, single-stepping, etc.?, Are there tools to help find bugs or perform static analysis?, How can ...

---

<div class="post-metadata">

**Author:** ![carlos.aguero](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@carlos.aguero](https://forum.inductiveautomation.com/u/carlos.aguero)\
**Post date:** [May 1, 2023, 5:43pm UTC](https://forum.inductiveautomation.com/t/item-in-nested-dictionary-in-a-list-cant-be-modified/73098/4 "2023-05-01T17:43:23Z")

</div>

wow! you are right. makes sense.. 😄  
so I will need to create a new variable during each loop or create a copy of the dictionary..  
Thanks!

---

<div class="post-metadata">

**Author:** ![carlos.aguero](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@carlos.aguero](https://forum.inductiveautomation.com/u/carlos.aguero)\
**Post date:** [May 1, 2023, 5:46pm UTC](https://forum.inductiveautomation.com/t/item-in-nested-dictionary-in-a-list-cant-be-modified/73098/5 "2023-05-01T17:46:42Z")

</div>

very useful. Actually it mention that I should be using **dict.copy()**  
Thank you!!!

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [May 1, 2023, 8:35pm UTC](https://forum.inductiveautomation.com/t/item-in-nested-dictionary-in-a-list-cant-be-modified/73098/6 "2023-05-01T20:35:36Z")

</div>

Note, if you have nested structures, you will need to deepcopy the object to get a completely detached copy.

> **[Python Shallow Copy and Deep Copy (With Examples)](https://www.programiz.com/python-programming/shallow-deep-copy)**
>
> In this article, you’ll learn about shallow copy and deep copy in Python with the help of examples.

---

<div class="post-metadata">

**Author:** ![carlos.aguero](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@carlos.aguero](https://forum.inductiveautomation.com/u/carlos.aguero)\
**Post date:** [May 1, 2023, 9:08pm UTC](https://forum.inductiveautomation.com/t/item-in-nested-dictionary-in-a-list-cant-be-modified/73098/7 "2023-05-01T21:08:54Z")

</div>

Just in time!! I was just going through that problem!...  
I used deepcopy as mentioned in your link and it worked = )  
Thank you!!
