1.2.24 · D1Introduction to Programming (Python)

Foundations — Dictionaries — key-value pairs, access, methods (keys, values, items, get, update)

1,952 words9 min readBack to topic

This page assumes you know nothing. Before you can read student = {"name": "Asha"} with understanding, you must first understand every mark on that line: the {, the :, the ,, the ", the =, and the ideas hiding behind words like hashable, view, and O(1). We build them in order, each one earned before the next.


0. The picture the whole topic lives in

Before symbols, hold this image: a wall of labelled drawers. Each drawer has a name tag on the front (the key) and one object inside (the value). You never open every drawer — you read the tag you want and pull that drawer.

Figure — Dictionaries — key-value pairs, access, methods (keys, values, items, get, update)

Keep this drawer wall in your head. Every symbol below is just a way of drawing, reading, or changing this wall in Python.


1. = — the assignment arrow (not "equals")

student = {"name": "Asha"}   # the note "student" now points at the whole drawer wall

Why the topic needs it: every dictionary example begins d = {...}. Without knowing = is "store into a name", the very first character of every code block is a mystery.


2. "..." — a string (text the computer holds literally)

Why the topic needs it: dictionary keys and values are constantly strings. d["name"] only works because "name" is a piece of text you can look up.


3. { }, : , , — the shape of a dictionary

Now we can read the container itself.

Figure — Dictionaries — key-value pairs, access, methods (keys, values, items, get, update)

Why the topic needs it: this is the notation of the whole chapter. Every single dictionary you will ever write is braces + colons + commas.


4. d[k] — the bracket lookup (reading one drawer)

student["name"]   # -> "Asha"   (open the "name" drawer)

Why the topic needs it: this is the payoff of the whole idea — pulling a value by its label. The parent's speed claim ("O(1)") is about this operation.


5. Mutable vs immutable — can a thing be changed after birth?

This is the hidden idea behind "keys must be hashable". We build it carefully.

Figure — Dictionaries — key-value pairs, access, methods (keys, values, items, get, update)

Why the topic needs it: a dictionary key must be a reliable, unchanging tag. That's why only immutable (stamped-coin) values can be keys — the next section shows the machinery.


6. Hashing — turning a tag into a drawer position

This is the engine of the whole chapter. It borrows from Hashing and Hash Tables.

Figure — Dictionaries — key-value pairs, access, methods (keys, values, items, get, update)

Why the topic needs it: this single idea explains both "keys must be hashable" and "why lookup is fast" — the two claims the parent leans on hardest.


7. O(1) and O(n) — the speed labels

From Big-O Notation — time complexity, but you only need the intuition here.

Why the topic needs it: the parent's headline — "that constant-time lookup is the single reason dictionaries exist" — is a statement about these two labels. beats , and hashing is what buys it.


8. Iteration and for k in d — walking the wall

Built on For loops and iteration.

Why the topic needs it: the everyday pattern for k, v in scores.items() and the common mistake "for x in d loops over values" both depend on knowing what a for loop over a dict actually yields.


Prerequisite map

Assignment = store into name

Dictionary literal braces colon comma

Strings quoted text

Bracket access d k

Immutable vs mutable

Hashing tag to position

Keys must be hashable

Fast lookup

Big-O O of 1 vs O of n

Dictionaries topic

For loops iteration

Loop yields keys, items yields pairs


Equipment checklist

Test yourself — cover the right side and answer aloud.

In Python, what does x = 5 actually do?
Sticks the name x onto a box holding the value 5 (store into a name, not "equals").
Why does "name" have quotes but name might not?
"name" is literal text (a string); name without quotes means "the value stored under the note name".
In {"age": 20}, what do the : and would-be , separate?
: joins a key to its value inside one pair; , separates one pair from the next.
What does d["name"] do, and what if the key is missing?
Opens the drawer tagged "name" and returns its value; if missing it crashes with KeyError.
Difference between mutable and immutable?
Mutable = changeable in place (list); immutable = frozen forever (tuple, str, int).
Why must a dictionary key be immutable/hashable?
Hashing needs the same tag to always give the same position; a changeable tag would change its hash and lose the value.
What does a hash function do for a dictionary?
Turns a key into a fixed number that points straight to its storage position — no scanning.
What do and mean?
= constant work regardless of size; = work grows with the number of items.
What does for k in d: give you each loop?
Each key in turn (not the values).

Connections

  • Yeh note Hinglish mein padho →
  • Lists — ordered mutable sequences
  • Tuples — immutable sequences
  • Sets — unique hashable elements
  • Hashing and Hash Tables
  • For loops and iteration
  • Big-O Notation — time complexity