githubEdit

Dictionaries

Dictionaries store key/value pairs. Keys must be unique and immutable.

Creating Dictionaries

empty = {}
person = {"name": "Jack", "age": 26}
mixed = {1: "apple", "colors": ["red", "green"]}

from_pairs = dict([(1, "a"), (2, "b")])

Accessing Values

print(person["name"])      # Jack
print(person.get("age"))   # 26
print(person.get("city"))  # None

Adding and Updating

person["age"] = 27
person["city"] = "Downtown"

Removing

Common Methods

Dictionary Comprehension

Membership and Iteration

Previous

Last updated