add a value to a Dictionary

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Sha6er

i made this dictionary

var inv_data = {0: {"Item": "10009", "Stack": 1}}

it print this value

{0:{Item:10009, Stack:1}}

0 is the key for 1st value
i want to add a new value
so it will check first if the key exist then it will add a new key and assign to it the new value
can anyone help me with this?

:bust_in_silhouette: Reply From: Thomas Karcher

It really looks like an array of dicts would be a better solution here:

var inv_data = [{"Item": "10009", "Stack": 1}]
print (inv_data[0])
inv_data.append("Item": "10010", "Stack": 1})
print (inv_data[1])

But if you really want to use a dict of dicts, you could do it like this:

var inv_data = {0: {"Item": "10009", "Stack": 1}}
print (inv_data[0])
inv_data[inv_data.size()] = {"Item": "10010", "Stack": 1}
print (inv_data[1])

it worked Thomas, Thank you very much.

Sha6er | 2021-04-27 00:58