Page8/20
Dictionaries & Sets · Page 1 of 2
Dictionaries
Dictionaries & Sets
Dictionaries — Key-Value Storage
Dictionaries are unordered (Python 3.7+: insertion-ordered) mappings from keys to values. They enable O(1) lookups — essential for large datasets.
student = {
"name": "Alice",
"age": 22,
"grades": [90, 85, 92],
"enrolled": True
}
Accessing & Modifying
student["name"] # "Alice"
student.get("gpa", 0.0) # 0.0 (safe default)
student["email"] = "a@b.com" # add key
del student["age"] # remove key
"name" in student # True
Iterating
for key, value in student.items():
print(f"{key}: {value}")
Data Science relevance: JSON APIs, configuration files, and Pandas'
to_dict()all return dictionaries.
main.py
Loading...
OUTPUT
▶Click "Run Code" to execute…