Maps in Prolog
On this page
Based on the input provided, the target language for the translation is Python. Below is the translation of the Go code example into Python, along with the explanation in Markdown format suitable for Hugo.
Maps
Maps are Python’s built-in associative data type (sometimes called dictionaries or dicts in Python).
To create an empty dictionary, use the builtin dict()
.
Set key/value pairs using typical name[key] = val
syntax.
Printing a dictionary with e.g. print()
will show all of its key/value pairs.
Get a value for a key with name[key]
.
If the key doesn’t exist, accessing it directly will raise a KeyError
. You can use the get
method which returns None
(or a specified default) if the key is not found.
The builtin len
returns the number of key/value pairs when called on a dictionary.
To remove key/value pairs from a dictionary, use the del
statement.
To remove all key/value pairs from a dictionary, use the clear
method.
You can also check if a key is present in the dictionary using the in
keyword.
You can declare and initialize a new dictionary in the same line with this syntax.
Note that dictionaries appear in the form {k: v, k: v}
when printed with print()
.
Next example: Functions.