Dict
Maps hashable objects to a value
Create
Create a dictionary
| Action |
Code |
Details |
|
Define empty dictionary
|
|
|
|
Define with string keys and values
|
x = {'color': 'blue', 'size': 'large'}
|
|
|
Define with integer keys
|
x = {1: 'a', 2: 'b', 3: 'c'}
|
|
|
Generate from comprehension over list of keys
|
keys = ['a', 'b', 'c']
{key: key.upper() for key in keys}
|
|
|
Define from keys iterator and constant value v
|
dict.fromkeys(key_iter, v)
|
|
|
Define dynamically from iterators for keys and values
|
x = dict(zip(key_iter, value_iter))
|
|
Test
| Action |
Code |
Details |
|
Is dict or subclass
|
|
|
|
Is dict but not subclass
|
|
|
|
Is empty
|
|
|
|
Is not empty
|
|
|
|
Contains key k
|
|
|
|
Contains value v
|
|
|
Contains value None
|
|
|
|
Contains specific keys
|
|
|
|
Contains keys keys
|
|
|
|
Contains specific values
|
{v1, v2}.issubset(x.values())
|
|
|
Contains values values
|
set(values).issubset(x.values())
|
|
|
Contains duplicate values
|
len(x) != len(set(x.values()))
|
|
| Action |
Code |
Details |
|
Number of key-value elements
|
|
|
|
Hash
|
hash(frozenset(x.items()))
|
|
|
Keys
|
|
As dict_keys |
|
Keys as list
|
|
|
|
Keys as sorted list
|
|
|
|
All keys with value v
|
[k for k in x if x[k] == v]
|
|
|
Values
|
|
As dict_values |
|
Values as list
|
|
|
|
Values sorted by keys
|
[x[k] for k in sorted(x)]
|
|
|
Values from list of keys
|
[x[k] for k in keys_list]
|
|
|
Entries (as list of tuples)
|
|
|
|
Value from key
|
|
Throws an error if missing |
|
Try get value from key
|
|
Returns None if missing |
|
Try get value from key, with default v if missing
|
|
|
|
First key with value v
|
next(k for k in x if x[k] == v)
|
|
|
Get index of key k
|
|
|
Update
| Action |
Code |
Details |
|
Update entry value
|
|
|
|
Update entry, error if missing
|
|
? |
|
Rename entry
|
|
|
Grow
| Action |
Code |
Details |
|
Add or update entry
|
|
|
|
Add entry with value if it does not exist
|
x.setdefault('size', 'medium')
|
|
Shrink
| Action |
Code |
Details |
|
Remove all entries (clear)
|
|
|
|
Remove entry by key
|
|
|
|
Remove entries by keys
|
|
|
|
Remove entry by key, get value
|
|
|
|
Remove keys, get values
|
|
|
|
Remove last-inserted entry, get value
|
|
|
Combine
Instead of merging several dictionaries, consider using a ChainMap, which is often much faster.
| Action |
Code |
Details |
|
Merge with another dictionary
|
|
|
|
Merge with another dictionary
|
|
|
Derive
| Action |
Code |
Details |
|
Copy
|
|
|
|
Copy
|
|
|
|
Subset for keys
|
|
Error if a key is missing |
|
Subset for keys with default v
|
{k: x.get(k, default=v) for k in keys}
|
|
|
Subset (intersection) for keys
|
{k: x[k] for k in x.keys() if k in keys}
|
Returns empty dict if all keys are missing |
|
Subset except for keys
|
{k: x[k] for k in x.keys() if k not in keys}
|
|
Combine
| Action |
Code |
Details |
|
Merge dictionaries
|
|
|
|
Merge dictionaries
|
|
|
Convert
| Action |
Code |
Details |
|
List of key-value tuples
|
|
|
|
JSON string
|
|
|
|
JSON string, handle non-string entries
|
json.dumps(x, default = str)
|
|
Iterate
| Action |
Code |
Details |
|
Loop over keys
|
|
|
|
Loop over values
|
|
|
|
Loop over key-value pairs
|
for key, value in x.items():
|
More efficient than a key-based for-loop as each x[k] lookup requires recomputation of the hash for k |
Show
| Action |
Code |
Details |
|
Pretty print
|
|
|