Skip to content

data.maps

look up, change, merge and transform maps of keys to values

A map pairs keys with values, written {name: "Ada", age: 36}. These functions look values up, change or merge maps, convert them to and from lists of pairs, and transform every value or key.

Tags: collections

  • keys the map’s keys, as a list
  • values the map’s values, as a list
  • get the value under key k, or default if there’s none
  • has whether the map has the key k
  • del remove key k and its value from the map, changing it, and return the map
  • merge a new map with every entry of every map
  • entries the map as a list of [key, value] pairs, the same as list(m)
  • from_entries a map from a list of [key, value] pairs, the reverse of entries
  • invert for a map, swap keys and values, so each value becomes a key (as a string)
  • map_values a new map with the same keys and f(value) for every value
  • filter keep only the entries that pass the test f, which is given each key and value
  • filter_keys keep only the entries whose key passes the test f
1
# map + map merges them, and the right side wins on shared keys
2
{a: 1} + {a: 2, b: 3}{a: 2, b: 3}
3
# m[k] += v on a missing key starts from 0, an empty string or an empty list, whichever fits v, and a missing map on the way is created
4
m = {}; m["a"] += 5; m{a: 5}
Example outputCtrl+Enter to run
keys(m: map) -> list

the map’s keys, as a list

1
{a: 1, b: 2}.keys["a", "b"]
Example outputCtrl+Enter to run

See also values

values(m: map) -> list

the map’s values, as a list

1
{a: 1, b: 2}.values[1, 2]
Example outputCtrl+Enter to run

See also keys

get(m: map, k: any, default?: any) -> any

the value under key k, or default if there’s none

1
{a: 1}.get("a")1
2
{a: 1}.get("b", 0)0
Example outputCtrl+Enter to run

See also has

has(m: map, k: any) -> bool

whether the map has the key k

1
{a: 1}.has("a")true
Example outputCtrl+Enter to run

See also get, contains

del(m: map, k: any) -> map

remove key k and its value from the map, changing it, and return the map

1
{a: 1, b: 2}.del("a"){b: 2}
Example outputCtrl+Enter to run

See also merge

merge(*maps: map) -> map

a new map with every entry of every map

When maps share a key, the later one’s value wins. Same as a + b

1
merge({a: 1, b: 2}, {b: 3}){a: 1, b: 3}
2
merge({a: 1}, {b: 2}, {a: 3}){a: 3, b: 2}
Example outputCtrl+Enter to run

See also del

entries(m: map) -> list

the map as a list of [key, value] pairs, the same as list(m)

1
{a: 1, b: 2}.entries[["a", 1], ["b", 2]]
Example outputCtrl+Enter to run

See also from_entries, list

from_entries(xs: list) -> map

a map from a list of [key, value] pairs, the reverse of entries

If a key repeats, the last one wins

1
[["a", 1], ["b", 2]].from_entries{a: 1, b: 2}
2
zip(["x", "y"], [1, 2]).from_entries{x: 1, y: 2}
Example outputCtrl+Enter to run

See also entries

invert(m: map) -> map

for a map, swap keys and values, so each value becomes a key (as a string)

1
{a: 1, b: 2}.invert{1: "a", 2: "b"}
Example outputCtrl+Enter to run

See also entries

map_values(m: map, f: fn) -> map

a new map with the same keys and f(value) for every value

1
{a: 1, b: 2}.map_values(|v| v * 10){a: 10, b: 20}
Example outputCtrl+Enter to run

See also map, filter

filter(m: map, f: fn) -> map

keep only the entries that pass the test f, which is given each key and value

1
{a: 1, b: 5}.filter(|k, v| v > 2){b: 5}
Example outputCtrl+Enter to run

Tags: searching

See also filter_keys, map_values

filter_keys(m: map, f: fn) -> map

keep only the entries whose key passes the test f

1
{a: 1, bb: 2}.filter_keys(|k| k.len > 1){bb: 2}
Example outputCtrl+Enter to run

Tags: searching

See also filter

1
# sum a map
2
{a: 1, b: 2}.values.sum3
3
# as [key, value] pairs
4
{a: 1, b: 2}.list[["a", 1], ["b", 2]]
5
# defaults with some overridden
6
{color: "red", size: 1} + {size: 3}{color: "red", size: 3}
7
# loop over entries
8
out = []; for [k, v] in {a: 1, b: 2}.entries { out.push("{k}={v}") }; out["a=1", "b=2"]
9
# count items, starting from an empty map
10
n = {}; for w in ["a", "b", "a"] { n[w] += 1 }; n{a: 2, b: 1}
11
# group items under a key
12
g = {}; for w in ["ant", "bee", "ape"] { g[w[0]] += [w] }; g{a: ["ant", "ape"], b: ["bee"]}
13
# nested maps appear as you fill them
14
t = {}; t["x"]["y"] += 1; t{x: {y: 1}}
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.