Using a YAML object of a parsed mapping

When a YAML document with mappings is parsed, it is not parsed as a dict but as a YAML object which behaves very similarly to a dict, but with some extra capabilities.

You can use .items(), .keys(), .values(), look up items with square bracket notation, .get(key, with_default_if_nonexistent) and use “x in y” notation to determine key membership.

To retrieve the equivalent dict (containing just other dicts, lists and strings/ints/etc.) use .data.

Example yaml_snippet:

a: 1
b: 2
c: 3
from strictyaml import Map, Int, load
from ensure import Ensure

schema = Map({"a": Int(), "b": Int(), "c": Int()})


Ensure(load(yaml_snippet, schema).is_mapping()).is_true()

Equivalence with equivalent plain dict:

Ensure(load(yaml_snippet, schema)).equals({"a": 1, "b": 2, "c": 3})


Ensure(load(yaml_snippet, schema).items()).equals([("a", 1), ("b", 2), ("c", 3)])

Use in to detect presence of a key:

Ensure("a" in load(yaml_snippet, schema)).is_true()


Ensure(load(yaml_snippet, schema).values()).equals([1, 2, 3])


Ensure(load(yaml_snippet, schema).keys()).equals(["a", "b", "c"])

Key lookup:

yaml = load(yaml_snippet, schema)

Dict lookup:

Ensure(load(yaml_snippet, schema)["a"]).equals(1)


Ensure(load(yaml_snippet, schema).get("a")).equals(1)

.get() nonexistent:

Ensure(load(yaml_snippet, schema).get("nonexistent")).equals(None)


Ensure(len(load(yaml_snippet, schema))).equals(3)

Executable specification

Page automatically generated from mapping-representation.story.