Merge 2 dictionaries or create a new dictionary from 2 other dictionaries

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

Hey everyone,

I am trying to either merge 2 dictionaries or create a new dictionary from 2 other dictionaries. There are no sub dict in either dictionary. They are identical except for the items inside. I have tried numerous methods with nothing working. I am using Godot 3.5. Any help would stop me from pulling my hair out lol.

Thanks!!

Did you try dict1.merge(dict2)

Enfyna | 2023-05-14 18:40

:bust_in_silhouette: Reply From: jgodfrey

You want the merge() method. The docs are here:

So, something like:

func _ready():
	var dict1 = { "a": 1, "b": 2, "c": 3 }
	var dict2 = { "x": 7, "y": 8, "z": 9 }
	dict1.merge(dict2)
	print(dict1)

prints: {a:1, b:2, c:3, x:7, y:8, z:9}

Note that merge() takes an optional second arg that tells it how to handle duplicate keys.