Maps in Fortress

Based on the provided input, the target language specified in fortress is Python. Here is the translated code and explanation formatted in Markdown suitable for Hugo:

Maps are Python’s built-in associative data type (sometimes called dictionaries).

To create an empty dictionary, use the dict constructor:

m = {}

Set key/value pairs using typical name[key] = val syntax.

m["k1"] = 7
m["k2"] = 13

Printing a dictionary with the print() function will show all of its key/value pairs.

print("map:", m)

Get a value for a key with name[key].

v1 = m["k1"]
print("v1:", v1)

If the key doesn’t exist, a KeyError is raised.

try:
    v3 = m["k3"]
except KeyError:
    v3 = 0
print("v3:", v3)

The built-in len() function returns the number of key/value pairs when called on a dictionary.

print("len:", len(m))

To remove a key/value pair from a dictionary, use the del statement.

del m["k2"]
print("map:", m)

To remove all key/value pairs from a dictionary, use the clear method.

m.clear()
print("map:", m)

The optional second return value when getting a value from a dictionary using the get method indicates if the key was present in the dictionary by returning None if it wasn’t found. Alternatively, you can check for the key using the in keyword.

prs = "k2" in m
print("prs:", prs)

You can also declare and initialize a new dictionary in the same line with this syntax.

n = {"foo": 1, "bar": 2}
print("map:", n)

Python’s collections module provides a number of useful utility functions for dictionaries.

from collections import Counter

n2 = {"foo": 1, "bar": 2}
if Counter(n) == Counter(n2):
    print("n == n2")

Note that dictionaries appear in the form {'k1': 7, 'k2': 13} when printed with print().

print("map:", m)