Maps
A map stores values under keys, like a dictionary stores a definition under each word.
ages: {str: int} = {"Alice": 30, "Bob": 25}
print(ages)
print(ages["Alice"])Output:
{"Alice": 30, "Bob": 25}
30Map types
The type of a map is written {key type: value type}:
| Type | Maps |
|---|---|
{str: int} | strings to ints |
{int: str} | ints to strings |
{str: [str]} | strings to lists of strings |
{int: {str: bool}} | ints to maps of strings to bools |
Keys can be int, byte, bool or str.
floatcan’t be a key, becausenanis not equal to itself, so it could never be found again.- Lists and maps can’t be keys, because they can change after being used as a key.
Values can be of any type, including lists and other maps.
An empty map {} gets its type from where it is used, so it must be declared first: print({}) is an error.
A map literal may span several lines, and a trailing comma is allowed:
capitals: {str: str} = {
"Estonia": "Tallinn",
"Finland": "Helsinki",
}
print(capitals["Estonia"])Output:
TallinnReading a value
map[key] gives the value stored under the key. If the key is not in the map, the program stops with a runtime error:
ages: {str: int} = {"Alice": 30}
print(ages["Carol"])Output:
example.luma:2:11: runtime error: key "Carol" not foundTo avoid the error, check first with has.
Checking for a key
has(map, key) is true if the key is in the map:
ages: {str: int} = {"Alice": 30}
if has(ages, "Carol") {
print(ages["Carol"])
} else {
print("no Carol")
}Output:
no CarolAdding and changing
map[key] = value adds the key if it is new, and replaces its value if it is already there:
ages: {str: int} = {"Alice": 30}
ages["Bob"] = 25 // new key
ages["Alice"] = 31 // replaces 30
print(ages)Output:
{"Alice": 31, "Bob": 25}Removing
remove(map, key) removes the key and its value. Removing a key that is not in the map stops the program with the same key not found error:
ages: {str: int} = {"Alice": 30, "Bob": 25}
remove(ages, "Alice")
print(ages)Output:
{"Bob": 25}Size
len(map) gives the number of keys:
ages: {str: int} = {"Alice": 30, "Bob": 25}
print(len(ages))Output:
2Order
A map remembers the order in which keys were added. Printing shows them in that order. Changing a key’s value keeps its place; a key that is removed and added again goes to the end:
m: {str: int} = {"a": 1, "b": 2, "c": 3}
m["a"] = 10
remove(m, "b")
m["b"] = 20
print(m)Output:
{"a": 10, "c": 3, "b": 20}Maps inside maps and lists
Values can be lists or maps, to any depth:
groups: {str: [str]} = {"admins": ["Ada"], "users": []}
groups["users"][] = "Guido"
print(groups)
people: [{str: float}] = [{"height": 1.8}]
people[0]["weight"] = 75
print(people)Output:
{"admins": ["Ada"], "users": ["Guido"]}
[{"height": 1.8, "weight": 75.0}]Assignment shares the map
Like lists, assigning a map to another variable doesn’t copy it. Both names refer to the same map:
a: {str: int} = {"x": 1}
b: {str: int} = a
b["y"] = 2
print(a)Output:
{"x": 1, "y": 2}Literal keys must be unique
A map literal can’t contain the same key twice when the keys are written directly:
m: {str: int} = {"a": 1, "a": 2} // error: duplicate key "a" in this map (first at 1:18)Going through all keys
ages.walk(name, age) { ... } runs a block for each key and its value, in the order the keys were added. See Walk loops.
Not available yet
- Maps can’t be compared with
==or!=.
See Limitations.