# Invalid key error but only if nested dictionary

**URL:** <https://forum.inductiveautomation.com/t/invalid-key-error-but-only-if-nested-dictionary/93539>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [July 22, 2024, 3:30pm UTC](https://forum.inductiveautomation.com/t/invalid-key-error-but-only-if-nested-dictionary/93539 "2024-07-22T15:30:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameronin](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Cameronin](https://forum.inductiveautomation.com/u/Cameronin)\
**Post date:** [July 22, 2024, 3:30pm UTC](https://forum.inductiveautomation.com/t/invalid-key-error-but-only-if-nested-dictionary/93539/1 "2024-07-22T15:30:29Z")

</div>

Hi humans  
I'm trying to build a dictionary from a couple of named queries to provide information on the products used in the batch orders processed during a shift.  
If I make a dictionary using the batch order codes as keys and the first piece of information I'm retrieving as values then it works fine.  
If I try to use the batch order codes as keys, and then start a nested dictionary for all the batch information to be associated with the batch order key, I get a script-Eval error for "invalid key error".  
(None of the batch order codes start with digits or contain special characters, vast majority are "BO" then 6 digits).

I don't understand why "BO000000" is a valid key for a {key:value} dictionary, but not for a {key:{key:value}} dictionary.

Code:

```python
halfDict = {}
countDict = {}
halfList = []
for row in boSet:
	if row[1] != None:
		isHalf = system.dataset.toPyDataSet(system.db.runNamedQuery("ProjectScript/isHalf",{'barcode':row[1].split("-")[0]}))
		halfDict[str(row[0]).strip(" ")] = isHalf[0][0]
		countDict[str(row[0]).strip(" ")]['isHalf'] = isHalf[0][0]
	else:
		halfDict[str(row[0]).strip(" ")] = "u"
		countDict[str(row[0]).strip(" ")]['isHalf'] = "u"

```

Code will happily make 'halfDict' and return it, but gives an invalid key error on the BO codes for 'countDict'.  
(The strip part doesn't actually make a difference, it's just to illustrate I've tried it...)

I've got the same dictionary setup code in another project and it's working fine.  
Any hints as to where I'm being daft greatly appreciated.

---

<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:** [July 22, 2024, 5:58pm UTC](https://forum.inductiveautomation.com/t/invalid-key-error-but-only-if-nested-dictionary/93539/2 "2024-07-22T17:58:46Z")

</div>

`halfDict[key]` assigns a value to that key like you would expect.  
On the other hand, `countDict[key]['isHalf']` tries to access that key, which raises the `KeyError`.  
This will NEVER work if `key` doesn't already exist in the dict.

There are solutions for this, like the dict `setdefault` method.

Why are you maintaining a second dict with the same keys ?  
And, more generally, what are you trying to do ?

---

<div class="post-metadata">

**Author:** ![Cameronin](https://avatars.discourse-cdn.com/v4/letter/c/9fc29f/32.png) [@Cameronin](https://forum.inductiveautomation.com/u/Cameronin)\
**Post date:** [July 23, 2024, 8:06am UTC](https://forum.inductiveautomation.com/t/invalid-key-error-but-only-if-nested-dictionary/93539/3 "2024-07-23T08:06:53Z")

</div>

Hi Pascal  
The other code isn't the same dictionary, it's just set up what I thought was the same way.  
But isn't obviously, because for some reason I kept missing the line that assigns the nested dictionary keys.  
Because I am a doofus.  
_headdesk_  
I'll take a look at `setdefault` as well.  
Thanks
