Skip to content

Latest commit

 

History

History
77 lines (66 loc) · 2.28 KB

lang-ref-dict.md

File metadata and controls

77 lines (66 loc) · 2.28 KB

Dictionaries

#@ color = {"red": 123, "yellow": 100, "blue": "245"}
red: #@ color["red"]

Copied here for convenience from Starlark specification.

x = {"one": 1, "two": 2}
x.clear()  # None
print(x)   # {}
x = {"one": 1, "two": 2}
x.get("one")       # 1
x.get("three")     # None
x.get("three", 0)  # 0
x = {"one": 1, "two": 2}
x.items()  # [("one", 1), ("two", 2)]
x = {"one": 1, "two": 2}
x.keys()  # ["one", "two"]
x = {"one": 1, "two": 2}
x.pop("one")       # 1
x                  # {"two": 2}
x.pop("three", 0)  # 0
x.pop("four")      # error: missing key
x = {"one": 1, "two": 2}
x.popitem()  # ("one", 1)
x.popitem()  # ("two", 2)
x.popitem()  # error: empty dict
x = {"one": 1, "two": 2}
x.setdefault("one")       # 1
x.setdefault("three", 0)  # 0
x                         # {"one": 1, "two": 2, "three": 0}
x.setdefault("four")      # None
x                         # {"one": 1, "two": 2, "three": None}
x = {}
x.update([("a", 1), ("b", 2)], c=3)
x.update({"d": 4})
x.update(e=5)
x  # {"a": 1, "b": "2", "c": 3, "d": 4, "e": 5}
x = {"one": 1, "two": 2}
x.values()  # [1, 2]