dict¶
A mutable mapping of keys to values, similar to Python's dict. Supports Python-style methods like get(), pop(), items(), keys(), and values().
Properties¶
| Name | Type | Description |
|---|---|---|
count |
int |
Gets the number of key/value pairs in the dictionary. |
Methods¶
get_string_key_entries() -> Iterable[KeyValuePair[str, object?]]¶
Projects this dictionary's entries as string-keyed pairs for
reflection-free JSON dispatch. Returns an empty sequence when
is not string.
fromkeys(keys: Iterable[K], value: V = default!) -> dict[K, V]¶
Create a new dictionary with keys from keys and values set to value.
Parameters:
keys(Iterable[K]) -- An iterable of keys for the new dictionary.value(V) -- The value for all keys. Defaults todefault(V).
Returns: A new dictionary with the specified keys and value.
copy() -> dict[K, V]¶
Return a shallow copy of the dictionary.
Returns: A new dictionary with the same key-value pairs.
clear()¶
Remove all items from the dictionary.
contains(key: K) -> bool¶
Check if key exists in the dictionary.
Used by the compiler for key in dict expressions.
get(key: K) -> Optional[V]¶
Return the value for key if present, otherwise
Optional{T}.None.
Parameters:
key(K) -- The key to look up.
Returns: An Optional{T} containing the value, or None.
get(key: K, @default: V) -> V¶
Return the value for key if present, otherwise default.
Parameters:
key(K) -- The key to look up.@default(V)
Returns: The value for the key, or the default.
items() -> DictItemsView[K, V]¶
Return a view of the dictionary's key-value pairs.
Returns: A view of (key, value) pairs.
keys() -> DictKeyView[K, V]¶
Return a view of the dictionary's keys.
Returns: A view of the keys.
pop(key: K) -> V¶
Remove the specified key and return the corresponding value.
Raises KeyError if the key is not found.
Parameters:
key(K) -- The key to remove.
Returns: The value that was associated with the key.
Raises:
KeyError-- Thrown if the key is not found.
pop(key: K, @default: V) -> V¶
Remove the specified key and return the corresponding value. If the key is not found, return default.
Parameters:
key(K) -- The key to remove.@default(V)
Returns: The removed value or the default.
set_default(key: K, @default: V) -> V¶
If key is in the dictionary, return its value. If not, insert key with default and return default.
Parameters:
key(K) -- The key to look up or insert.@default(V)
Returns: The existing or newly inserted value.
update(other: IReadOnlyDictionary[K, V])¶
Update the dictionary with key-value pairs from other, overwriting existing keys.
Parameters:
other(IReadOnlyDictionary[K, V]) -- A dictionary whose pairs are merged in.
update(v: IEnumerable<(K,)¶
Update the dictionary with key-value pairs from an iterable of tuples.
values() -> DictValuesView[K, V]¶
Return a view of the dictionary's values.
Returns: A view of the values.
remove(key: K)¶
Removes the item with the specified key from the dictionary.
Raises:
KeyError-- Thrown if the key does not exist.
to_dictionary() -> Dictionary[K, V]¶
Convert to a standard .NET Dictionary.
merge(other: dict[K, V]) -> dict[K, V]¶
Returns a new dictionary that is the result of merging this dictionary with other. Keys from other take precedence.
fromkeys(keys: Iterable[TKey]) -> dict[TKey, object]¶
Create a new dictionary with keys from keys and values set to null.
Mirrors Python's dict.fromkeys(iterable).
Parameters:
keys(Iterable[TKey]) -- An iterable of keys for the new dictionary.
Returns: A new dictionary with the specified keys all mapped to null.
fromkeys(keys: Iterable[TKey], value: TValue) -> dict[TKey, TValue]¶
Create a new dictionary with keys from keys and values set to value.
Mirrors Python's dict.fromkeys(iterable, value). All keys share the same value reference.
Parameters:
keys(Iterable[TKey]) -- An iterable of keys for the new dictionary.value(TValue) -- The value assigned to every key.
Returns: A new dictionary with the specified keys all mapped to value.
add(key: K, value: V)¶
For collection initializers.