dict

Store values under keys and read them by key. Keys keep insertion order; writing an existing key replaces its value without moving the key. Each key has one value.

Syntax

dict(mapping_or_pairs=(), /, **items)

Returns

Example

settings = dict(mode="farm", retries=3)
mode = settings.get("mode")   # returns farm
keys = settings.keys()

for key in settings:
    text(str(settings[key]))   # returns None

Methods

17 methods

dict.constructor

Create an empty dictionary or copy another dictionary or a source of (key, value) pairs. Named arguments add entries afterward and replace matching keys. Nested values are shared with the source.

Syntax

dict()
dict(mapping_or_pairs, /)
dict(key=value, ...)

Parameters

NameDescription
mapping_or_pairsdict | Iterable
A dict or a source you can loop through; each item must be a list or tuple with exactly two items: key and value.
Depends on overload · Positional only
Default: ()
key=valueAny?
Entries written as key=value, added after the source; matching keys are replaced.
Optional · Variadic
No default

Returns

dict — A new mutable dictionary.

Example

settings = dict(mode="farm", retries=3)
copied = dict(settings)
from_pairs = dict([("mode", "pvp")])

dict.literal

Create a dictionary by writing key: value pairs between braces. Repeated keys keep the last value.

Syntax

{"mode": "farm", "retries": 3}

Parameters

NameDescription
keyAny
Key identifying the value. Numbers, strings and tuples of valid keys are allowed; lists, dictionaries and sets are not. True, 1 and 1.0 identify the same key.
Required · Positional or named
No default
valueAny?
Value stored for that key.
Required · Positional or named
No default

Returns

dict — A new dictionary that preserves the literal entry order.

Example

settings = {
    "mode": "farm",
    "retries": 3,
    "enabled": True
}

dict.comprehension

Build a dictionary from key: value pairs calculated while looping through a source. Repeated keys replace earlier values. Use if to filter items; multiple for/if clauses are allowed. Loop variables stay inside this expression.

Syntax

{key: value for target in iterable if condition}

Parameters

NameDescription
keyAny
dict key calculated for each accepted item.
Required · Positional or named
No default
valueAny?
Value stored for that key.
Required · Positional or named
No default
targetName | destructuring target
Variable or group of variables receiving the current item. A target such as *rest is not supported.
Required · Positional or named
No default
iterableIterable
Source you can loop through, such as a string, collection or generator; a dictionary yields its keys.
Required · Positional or named
No default
conditionBoolean
Optional filter checked before storing an entry.
Optional · Positional only
Default: True

Returns

dict — A new mutable dictionary.

Example

pairs = [("mode", "farm"), ("retries", 3)]
settings = {key: value for key, value in pairs}

dict.key_access

Read the value stored under key. A missing key raises KeyError; use get() to supply a default instead.

Syntax

dictionary["key"]

Parameters

NameDescription
dictionarydict
The source dictionary.
Required · Positional or named
No default
keyAny
Key to look up.
Required · Positional or named
No default

Returns

Any? — The stored value, which may be None. A missing key raises KeyError.

Example

settings = {"mode": "farm", "retries": 3}
mode = settings["mode"]        # returns farm
missing = settings.get("unknown")  # returns None

dict.key_assignment

Write or replace one key/value entry with bracket assignment syntax.

Syntax

dictionary[key] = value

Parameters

NameDescription
dictionarydict
The mutable dictionary to update.
Required · Positional or named
No default
keyAny
Key to write or replace.
Required · Positional only
No default
valueAny?
Value to store for that key.
Required · Positional only
No default

Returns

None — No direct return value. The dictionary is updated in place.

Example

settings = {"mode": "farm"}
settings["mode"] = "pvp"

dict.length

Count how many key/value pairs are in the dictionary.

Syntax

len(dictionary, /)

Parameters

NameDescription
dictionarydict
The dictionary to count.
Required · Positional only
No default

Returns

Int — Number of entries in the dictionary.

Example

settings = {"mode": "farm", "retries": 3}
count = len(settings)          # returns 2

dict.get_method

Read a value by key and optionally fall back to a default.

Syntax

dictionary.get(key, default=None, /)

Parameters

NameDescription
keyAny
Key to look up.
Required · Positional only
No default
defaultAny?
Fallback value returned when the key is missing.
Optional · Positional only
Default: None

Returns

Any? — The stored value for that key, or the provided default.

Example

settings = {"mode": "farm"}
mode = settings.get("mode")           # returns farm
retry = settings.get("retries", 0)    # returns 0

dict.items_method

Return a live view of (key, value) tuples in insertion order. It reflects later dictionary changes. Loop over it or convert with list() to use indexes.

Syntax

dictionary.items()

Returns

dict_items — A dict_items view; it cannot be indexed directly.

Example

settings = {"mode": "farm"}
pairs = settings.items()

dict.pop_method

Remove a key and return its value. If the key is absent, return an explicitly supplied default, including None; omitting default raises KeyError.

Syntax

dictionary.pop(key, /)
dictionary.pop(key, default, /)

Parameters

NameDescription
keyAny
Key to remove.
Required · Positional only
No default
defaultAny?
Value returned for a missing key only when this argument is supplied. Omitting it raises KeyError for a missing key.
Depends on overload · Positional only
No default

Returns

Any? — Removed value, or the explicitly supplied default when the key is absent.

Example

settings = {"mode": "farm", "retries": 3}
removed = settings.pop("retries")

dict.copy_method

Return a new dictionary with the same entries. Adding, removing or replacing entries does not change the original; nested collections are still shared.

Syntax

dictionary.copy()

Returns

dict — A new dict; nested collections are not copied.

Example

settings = {"mode": "farm"}
copied = settings.copy()

dict.update_method

Add entries to this dictionary from another dictionary or a source of (key, value) pairs, then apply named arguments. Matching keys are replaced without changing their position.

Syntax

dictionary.update(other=(), /, **items)

Parameters

NameDescription
otherdict | Iterable
A dict or a source of two-item lists or tuples containing a key and value.
Optional · Positional only
Default: ()
itemsAny?
Entries written as key=value; these replace matching entries from other.
Optional · Variadic
No default

Returns

None — No direct return value. The dictionary is updated in place.

Example

settings = {"mode": "farm"}
settings.update([("retries", 3)], mode="pvp")

dict.setdefault_method

Return an existing value, or store and return the default when the key is missing.

Syntax

dictionary.setdefault(key, default=None, /)

Parameters

NameDescription
keyAny
Key to read or create.
Required · Positional only
No default
defaultAny?
Value stored when the key is absent.
Optional · Positional only
Default: None

Returns

Any? — Existing value or inserted default.

Example

settings = dict()
mode = settings.setdefault("mode", "farm")

dict.keys_method

Return a live view of keys in insertion order. It reflects later dictionary changes. Loop over it or convert with list() to use indexes.

Syntax

dictionary.keys()

Returns

dict_keys — A dict_keys view; it cannot be indexed directly.

Example

settings = {"mode": "farm", "retries": 3}
keys = settings.keys()
first_key = list(keys)[0]                 # returns mode

dict.values_method

Return a live view of values in key insertion order. It reflects later dictionary changes. Loop over it or convert with list() to use indexes.

Syntax

dictionary.values()

Returns

dict_values — A dict_values view; it cannot be indexed directly.

Example

settings = {"mode": "farm", "retries": 3}
values = settings.values()
last_value = list(values)[-1]             # returns 3

dict.clear_method

Remove every key/value pair from the dictionary.

Syntax

dictionary.clear()

Returns

None — No direct return value. The dictionary becomes empty.

Example

settings = {"mode": "farm", "retries": 3}
settings.clear()
text(str(len(settings)))   # returns 0

dict.for_loop

Loop through dictionary keys in insertion order, including keys added after creation.

Syntax

for key in dictionary:

Parameters

NameDescription
keyAny
Current key inside the loop.
Required · Positional or named
No default
dictionarydict
The dictionary to iterate.
Required · Positional or named
No default

Returns

None — No direct return value. The loop runs once per key.

Example

settings = {"mode": "farm", "retries": 3}
for key in settings:
    text(str(settings[key]))   # returns None

dict.truthy_check

Check whether the dictionary has at least one entry. An empty dictionary is treated as false.

Syntax

if dictionary | if not dictionary

Parameters

NameDescription
dictionarydict
The dictionary used in the condition.
Required · Positional or named
No default

Returns

Boolean condition — true when the dictionary is not empty, false when it is empty.

Example

settings = {}
if not settings:
    text("No settings")       # returns None