How to add a dictionary key?

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

Suppose we have

var Dict = {"A":{"TITLE1":"I am title 1."}}

How could I add another entry to Dict[A]? I cannot do:

var Dict = {"A":{"TITLE1":"I am title 1.","TITLE2":"I am title 2."}}
1 Like
:bust_in_silhouette: Reply From: jgodfrey

Looks like you’re trying to add a sub-key. This should give you an idea of how dicts work:

func _ready():
	var Dict = {"A":{"TITLE1":"I am title 1."}}
	Dict["A"]["TITLE2"] = "I am title 2"

Just another quick question which I am struggling, can I add more than 1 subkey at the same time?

I am trying to add a new subkey, but it keeps replacing it:

var Lib= {Page:{Category:CatName}}
Lib[Page][Category] = Content

I was trying to do category = {“A”,“B”,“C”} but always end up with just category = {“C”} the last value added. But I see that by your example the concept works, must be something bad in my code structure.

Upon experimenting with dictionaries today after trying to fix a bug I had for 3 days -.-, I finally found the problem.

If you set a dictionary like:

var TITLE = loadedDate[0][2]#Just a example of a array stored with the Title

Library[TITLE_CATEGORY][TITLE] = "Subtitle"

It will return a error like:

##Invalid set index ‘TITLE’ (on base: ‘String’) with value of type ‘String’.
Because TITLE was set only as a string, the loadedDate[0][comment0-2] is just a string. So I was trying to add a dictionary subkey to a string, :stuck_out_tongue:

What I had to do with my code was:

Library[TITLE_CATEGORY] = {TITLE:"Subtitle"}

Now my TITLE is a dictionary and can be referenced through


Don’t know why that was so hard to understand at first :smiley:

The_Black_Chess_King | 2020-05-15 22:43

1 Like