Python Dictionaries
dict Topic Foundation

Dictionary — key → value mappings

Dictionaries store values indexed by unique keys. Use them for fast lookup, structured records, and counting.

Picture this: a labeled locker

A dict maps a key to a value. Look up by name, not by position.

key"apple"
→
value3
→
lookupd["apple"]

Keys must be unique. Values can repeat.

In simple words

A dictionary is like a mini-database row: each key (like "name" or "age") is mapped to a value. Keys are unique and usually immutable (strings, numbers, tuples). Access values by their key: person['name'].

Diagram — key → value map

"Ada" key 3 value (count) phone["Riya"] fast lookup by key

Where dicts are used

Dictionaries are used for JSON-like records, frequency counters, configuration maps, and whenever you need O(1) lookup by a label instead of by position.

Key facts

  • Unordered (in concept) — insertion order is preserved in modern Python but don't rely on indexing.
  • Mapped by keys — access with mydict[key] or safe access with mydict.get(key, default).
  • Mutable — add, update, or remove entries.
  • Keys are unique — assigning the same key replaces the value.

Common types

  • Keys: strings, numbers, tuples (immutable).
  • Values: any Python object (lists, dicts, numbers, strings).
  • Use: mapping student_id → record, word → count, config keys → values.
Interactive visualizer — click a key to inspect

Simple dict visualizer

Click a pair to view details
Common operations
Click an operation above

Syntax reference

1person = {'name': 'Asha', 'age': 24, 'city': 'Bangalore'}
2print(person['name']) # 'Asha'
3print(person.get('job', 'Unknown'))
4person['age'] = 25 # update

Dictionaries — Topic Quiz

Check understanding

Answer all questions, then click Check answers. Use this before Practice in the editor.

Q1. What does a dictionary store?

Q2. How do you read a value for key k?

Q3. Are dictionary keys usually unique?

Q4. Word frequency is best stored in a…

Score: —