Concepts 🚀 Project 🧮 Labs
🧮 Labs / Python / Dictionaries — key/value storage

Dictionaries — key/value storage

dicts map keys to values. O(1) lookup. Insertion-ordered since Python 3.7.

script.py v3.1
Python output click Run to execute
Click "Run" to execute the code on the left.
The output will appear here.
🧠 How it works

dicts are hash tables — keys must be hashable (str, int, tuple, frozenset; NOT list or dict). person['key'] raises KeyError if missing. person.get('key', default) returns default if missing. items() yields (key, value) pairs. keys() yields keys. values() yields values. del d[k] removes key k. d.pop(k) removes and returns the value. Dict comprehension: {k_expr: v_expr for item in iter if cond}. Useful: dict(zip(keys, values)) builds a dict from two lists. Counter, defaultdict, OrderedDict in collections module for special cases.

✏️ Try these changes
  • Change person['name'] to person['phone']
    → KeyError
  • Change .get('phone', 'N/A') to .get('phone')
    → returns None instead of 'N/A'
  • Add 'skills' to the iteration filter — skip non-string values
    → more complex iteration
  • Change squares to {n: n*n for n in range(10) if n % 2 == 0}
    → only even squares