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 |
|---|---|---|
is_read_only |
bool |
Gets a value indicating whether the dictionary is read-only. Always false. |
count |
int |
Gets the number of key/value pairs in the dictionary. |
Methods¶
get_string_key_entries() -> IEnumerable[KeyValuePair[str, object | None]]¶
Projects this dictionary's entries as string-keyed pairs for
reflection-free JSON dispatch. Returns an empty sequence when
is not str.
fromkeys(keys: IEnumerable[K], value: V) -> dict[K, V]¶
Create a new dictionary with keys from keys and values set to value.
Parameters:
keys(IEnumerable[K]) -- An iterable of keys for the new dictionary.value(V) -- The value for all keys.
Returns: A new dictionary with the specified keys and value.
fromkeys(keys: IEnumerable[K]) -> dict[K, object]¶
Create a new dictionary with keys from keys, every value None —
python's valueless dict.fromkeys(keys). The values are typed object, since
None is not a value of every V.
Parameters:
keys(IEnumerable[K]) -- An iterable of keys for the new dictionary.
Returns: A new dictionary with the specified keys, all mapped to None.
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) -- The fallback value.
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) -- Value to return if key is not found.
Returns: The removed value or the default.
pop_item(last: bool = True) -> tuple[K, V]¶
Remove and return a (key, value) pair from the dictionary.
Parameters:
last(bool) -- IfTrue, remove the last pair; otherwise the first.
Returns: A tuple of the removed key and value.
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) -- The value to insert if key is absent.
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(other: IEnumerable[tuple[K, V]])¶
Update the dictionary with key-value pairs from an iterable of tuples.
Parameters:
other(IEnumerable[tuple[K, V]]) -- An iterable of(key, value)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.
update(other: dict[K, V])¶
Returns a new dictionary that is the result of merging this dictionary with other. Keys from other take precedence.
merge(other: dict[K, V]) -> dict[K, V]¶
fromkeys(keys: IEnumerable[TKey]) -> dict[TKey, object]¶
Create a new dictionary with keys from keys and values set to None.
Mirrors Python's dict.fromkeys(iterable).
Parameters:
keys(IEnumerable[TKey]) -- An iterable of keys for the new dictionary.
Returns: A new dictionary with the specified keys all mapped to None.
fromkeys(keys: IEnumerable[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(IEnumerable[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.