Dictionaries are associative containers that contain values referenced by unique keys. Dictionaries will preserve the insertion order when adding new entries. In other programming languages, this data structure is often referred to as a hash map or an associative array.
You can define a dictionary by placing a comma-separated list of key: value
pairs inside curly braces {}
.
Creating a dictionary:
You can access a dictionary's value by referencing its corresponding key. In the above example, points_dict["White"]
will return 50
. You can also write points_dict.White
, which is equivalent. However, you'll have to use the bracket syntax if the key you're accessing the dictionary with isn't a fixed string (such as a number or variable).
In the above code, points
will be assigned the value that is paired with the appropriate color selected in my_color
.
Dictionaries can contain more complex data:
To add a key to an existing dictionary, access it like an existing key and assign to it:
Finally, dictionaries can contain different types of keys and values in the same dictionary:
The keys of a dictionary can be iterated with the for
keyword:
Constructs an empty Dictionary.
Returns the same dictionary as from
. If you need a copy of the dictionary, use duplicate.
Returns true
if the two dictionaries do not contain the same keys and values.
Returns true
if the two dictionaries contain the same keys and values. The order of the entries does not matter.
Note: In C#, by convention, this operator compares by reference. If you need to compare by value, iterate over both dictionaries.
Returns the corresponding value for the given key
in the dictionary. If the entry does not exist, fails and returns null
. For safe access, use get or has.
Clears the dictionary, removing all entries from it.
Creates and returns a new copy of the dictionary. If deep
is true
, inner Dictionary and Array keys and values are also copied, recursively.
Removes the dictionary entry by key, if it exists. Returns true
if the given key
existed in the dictionary, otherwise false
.
Note: Do not erase entries while iterating over the dictionary. You can iterate over the keys array instead.
Finds and returns the first key whose associated value is equal to value
, or null
if it is not found.
Note: null
is also a valid key. If inside the dictionary, find_key may give misleading results.
Returns the corresponding value for the given key
in the dictionary. If the key
does not exist, returns default
, or null
if the parameter is omitted.
Gets a value and ensures the key is set. If the key
exists in the dictionary, this behaves like get. Otherwise, the default
value is inserted into the dictionary and returned.
Returns true
if the dictionary contains an entry with the given key
.
In GDScript, this is equivalent to the in
operator:
Note: This method returns true
as long as the key
exists, even if its corresponding value is null
.
Returns true
if the dictionary contains all keys in the given keys
array.
Returns a hashed 32-bit integer value representing the dictionary contents.
Note: Dictionaries with the same entries but in a different order will not have the same hash.
Note: Dictionaries with equal hash values are not guaranteed to be the same, because of hash collisions. On the contrary, dictionaries with different hash values are guaranteed to be different.
Returns true
if the dictionary is empty (its size is 0
). See also size.
Returns true
if the dictionary is read-only. See make_read_only. Dictionaries are automatically read-only if declared with const
keyword.
Returns the list of keys in the dictionary.
Makes the dictionary read-only, i.e. disables modification of the dictionary's contents. Does not apply to nested content, e.g. content of nested dictionaries.
Adds entries from dictionary
to this dictionary. By default, duplicate keys are not copied over, unless overwrite
is true
.
Note: merge is not recursive. Nested dictionaries are considered as keys that can be overwritten or not depending on the value of overwrite
, but they will never be merged together.
Returns the number of entries in the dictionary. Empty dictionaries ({ }
) always return 0
. See also is_empty.
Returns the list of values in this dictionary.