Python Dictionary Class 11: Key-Value Pairs, Methods, Traversal and Character Frequency
Updated 17 October 2026 · Fact-checked
A dictionary is a mutable collection of key-value pairs written in curly brackets, such as d = {"pen": 10}. Keys must be unique and immutable (numbers, strings, tuples); a value is read with d[key], which raises KeyError for a missing key, or d.get(key), which returns None or a default instead. Assigning d[key] = value adds a new pair or changes an existing one.
Each key maps to one value; get reads a value by key, items gives pairs, and assigning a new key adds a pair.
Illustration drawn for QuestionBuddy.
Understand Dictionaries: Keys, Values, get, items, update and Counting Characters (CBSE 11)
Creating. d = {"Maths": 90, "Sci": 85}; {} is the empty dictionary. Keys must be unique: in {"a": 1, "a": 2} the later value wins and the result is {'a': 2}. Keys must be immutable (int, float, str, tuple); a list cannot be a key. Values can be of any type. len(d) is the number of pairs. From Python 3.7 a dictionary keeps the order in which keys were added.
Access. d["Sci"] gives 85; a missing key raises KeyError. d.get("Eng") returns None for a missing key, and d.get("Eng", 0) returns the default 0. The test "Sci" in d checks KEYS only, not values.
Add, change, delete.
d["Eng"] = 78adds the pair if "Eng" is new, otherwise replaces its value.del d["Maths"]removes a pair (KeyError if absent);d.pop(key)removes it and returns the value;d.popitem()removes and returns the last pair added;d.clear()empties the dictionary.d.update(other)adds the pairs of other, replacing values of keys already present.
Views for traversal. d.keys(), d.values() and d.items() give the keys, the values and the (key, value) pairs; wrap them in list() to print them as lists. for k in d: loops over the keys, and for k, v in d.items(): unpacks each pair.
More methods. dict.fromkeys(keys, v) makes a dictionary with the given keys all set to v. d.setdefault(k, v) returns d[k] if k exists, otherwise it adds k with value v and returns v. d.copy() makes a shallow copy; e = d makes another name for the same dictionary.
Counting characters.
s = "banana"
freq = {}
for ch in s:
if ch in freq:
freq[ch] += 1
else:
freq[ch] = 1
print(freq) # {'b': 1, 'a': 3, 'n': 2}Dictionaries: Keys, Values, get, items, update and Counting Characters (CBSE 11): key facts
- Access
- d[k] gives KeyError if missing
- d.get(k) gives None
- d.get(k, default) gives default
- k in d tests keys
- in never tests values.
- Change
- d[k] = v adds or replaces
- del d[k] removes
- d.pop(k) removes and returns
- d.update(e) merges
- Duplicate keys are not allowed.
- Views
- keys()
- values()
- items() give (key, value) pairs
- wrap in list() to print
- for k, v in d.items() unpacks pairs.
- Other methods
- dict.fromkeys(keys, v)
- setdefault(k, v)
- copy()
- len(d) pairs
- e = d is an alias, not a copy.
How to answer Dictionaries: Keys, Values, get, items, update and Counting Characters (CBSE 11) questions
To trace dictionary code, keep a written copy of the dictionary and update it after every statement.
- 1Write the dictionary with its pairs in the order of insertion.
- 2For d[k] = v check whether k is already a key: replace the value if it is, add a pair at the end if not.
- 3For a loop write the current key, value and any counter on each pass.
- 4For get(), pop() and setdefault() note both the value returned and the change made to the dictionary.
- 5Print the dictionary with braces, quotes on string keys and a colon after each key.
Dictionaries: Keys, Values, get, items, update and Counting Characters (CBSE 11) in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
A shop price list
Suppose a canteen stores prices as price = {"tea": 10, "samosa": 15}. A new item "juice" costs 30, and the price of tea rises to 12.
Write the statements and the final dictionary.
Try it first, then show the solutionSolution
- price["juice"] = 30 adds a new pair.
- price["tea"] = 12 changes the value of an existing key.
Answer: {'tea': 12, 'samosa': 15, 'juice': 30}
Memory hook: Same statement adds or changes depending on whether the key exists.
Tricks to remember Dictionaries: Keys, Values, get, items, update and Counting Characters (CBSE 11)
Short memory aids. Say each one aloud once, then use it on a question the same day.
Keys are the index
A dictionary is looked up by key, like a word in a dictionary; the value is the meaning.
Use it when: Access and in-tests.
in looks at keys
"x" in d asks about keys only.
Use it when: Membership questions.
get is gentle
d[k] shouts KeyError; d.get(k) quietly returns None or your default.
Use it when: Counting and lookup programs.
Dictionaries: Keys, Values, get, items, update and Counting Characters (CBSE 11): solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Membership test · 1 mark · MCQBoard level
For d = {"a": 1, "b": 2}, what is the output of print(1 in d)?
(a) True
(b) False
(c) Error
(d) 0
Try it first, then show the solutionSolution
- The in operator checks keys only.
- The keys are "a" and "b", so 1 is not a key.
Answer: (b) False
Example 2: Add, change, delete · 3 marks · Short answerBoard level
Give the output:
d = {"Maths": 90, "Sci": 85}
d["Eng"] = 78
d["Sci"] = 88
del d["Maths"]
print(d, len(d))Try it first, then show the solutionSolution
- After d["Eng"] = 78 a new pair is added at the end.
- d["Sci"] = 88 replaces the value of the existing key.
- del removes the Maths pair, leaving Sci and Eng.
Answer: {'Sci': 88, 'Eng': 78} 2
Example 3: Methods trace · 4 marks · Short answerHarder board level
Give the output of all the print statements. (4 marks)
d = {"x": 1, "y": 2, "z": 3}
print(list(d.keys()), list(d.values()))
print(list(d.items()))
print(d.get("w"), d.get("w", 0))
d.update({"y": 20, "w": 4})
print(d)
print(d.pop("x"), d)Try it first, then show the solutionSolution
- keys are x, y, z and values are 1, 2, 3: ['x', 'y', 'z'] [1, 2, 3].
- items gives pairs: [('x', 1), ('y', 2), ('z', 3)].
- get('w') returns None because w is absent; with default 0 it returns 0.
- update replaces y with 20 and adds w: {'x': 1, 'y': 20, 'z': 3, 'w': 4}.
- pop('x') returns 1 and removes it: 1 {'y': 20, 'z': 3, 'w': 4}.
Answer: ['x', 'y', 'z'] [1, 2, 3]
[('x', 1), ('y', 2), ('z', 3)]
None 0
{'x': 1, 'y': 20, 'z': 3, 'w': 4}
1 {'y': 20, 'z': 3, 'w': 4}
Example 4: Frequency and setdefault · 5 marks · Short answerHOTS / competency
Trace the code, give the output, and say why print(freq["z"]) would fail and what to use instead. (5 marks)
s = "banana"
freq = {}
for ch in s:
if ch in freq:
freq[ch] += 1
else:
freq[ch] = 1
print(freq)
d = dict.fromkeys(["a", "b"], 0)
print(d.setdefault("c", 5), d.setdefault("a", 9))
print(d)Try it first, then show the solutionSolution
- b is new: {'b': 1}; a is new: {'b': 1, 'a': 1}; n is new, then a, n, a add 1 each: a becomes 3 and n becomes 2.
- freq prints {'b': 1, 'a': 3, 'n': 2}.
- fromkeys gives {'a': 0, 'b': 0}. setdefault('c', 5) adds c with 5 and returns 5; setdefault('a', 9) finds a already present and returns 0 without changing it.
- print shows 5 0, then d = {'a': 0, 'b': 0, 'c': 5}.
- freq["z"] raises KeyError because z is not a key; use freq.get("z", 0) to get 0.
Answer: {'b': 1, 'a': 3, 'n': 2}
5 0
{'a': 0, 'b': 0, 'c': 5}; freq["z"] raises KeyError, use freq.get("z", 0)
Example 5: Traversing items · 4 marks · Short answerHarder board level
Give the output:
prices = {"pen": 10, "book": 120, "bag": 450}
total = 0
for k, v in prices.items():
total += v
if v > 100:
print(k)
print(total)(4 marks)
Try it first, then show the solutionSolution
- pen: total 10, not above 100.
- book: total 130, 120 > 100 so book is printed.
- bag: total 580, 450 > 100 so bag is printed.
- After the loop print(total) shows 580.
Answer: book
bag
580
Where students lose marks in Dictionaries: Keys, Values, get, items, update and Counting Characters (CBSE 11)
The slips examiners see most often, and what to write instead.
Mistake: Using a list as a key
Not knowing that keys must be immutable.
Wrong working
d = {[1, 2]: "pair"} # TypeError: unhashable type
Right working
d = {(1, 2): "pair"}
Do this: Use a tuple, number or string as the key.
Usually costs: 1 mark
Mistake: Expecting in to check values
Treating a dictionary like a list.
Wrong working
d = {"a": 1}; 1 in d is True
Right working
1 in d is False; 1 in d.values() is True
Do this: in tests keys.
Usually costs: 1 mark
Mistake: Reading a missing key with square brackets
Not planning for absent keys.
Wrong working
freq[ch] += 1 for a new character raises KeyError
Right working
freq[ch] = freq.get(ch, 0) + 1
Do this: Use get() or test with in first.
Usually costs: 1 to 2 marks
Exam tips
- Always write braces, colons and quotes in dictionary output.
- Say that keys are unique and immutable when asked for the characteristics.
- Give both the value returned and the effect on the dictionary for pop and setdefault.
- Wrap keys(), values() and items() in list() when printing them.
Practice questions on Dictionaries: Keys, Values, get, items, update and Counting Characters (CBSE 11)
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
Which of these can be used as a key in a dictionary?
(a) [1, 2]
(b) (1, 2)
(c) {1, 2}
(d) {"a": 1}Hint
Keys must be immutable.
Answer
(b) (1, 2)
Q2 · 1 mark · Assertion-reasonHarder board level
Assertion (A): For a dictionary d, d["k"] raises an error when "k" is absent, but d.get("k") does not.
Reason (R): get() returns None, or a given default, for a missing key.
(a) Both A and R are true, and R is the correct explanation of A.
(b) Both A and R are true, but R is not the correct explanation of A.
(c) A is true, but R is false.
(d) A is false, but R is true.Hint
KeyError.
Answer
(a) Both A and R are true, and R is the correct explanation of A.
Q3 · 4 marks · Short answerHarder board level
Give the output:
d = {1: "a", 2: "b"} d[3] = "c" d[1] = "z" print(d, len(d), 2 in d, "b" in d) print(list(d.keys()))(4 marks)
Hint
Keys 1, 2, 3 only.
Answer
d[3] adds a pair; d[1] replaces the value. d is {1: 'z', 2: 'b', 3: 'c'}, length 3; 2 is a key: True; 'b' is a value, not a key: False. Output: {1: 'z', 2: 'b', 3: 'c'} 3 True False, then [1, 2, 3]
Q4 · 4 marks · Short answerHarder board level
Write a program that finds the student with the highest marks in marks = {"Ali": 72, "Bea": 91, "Cy": 85} and prints the name and marks. Give the output. (4 marks)
Hint
Loop over items with a best-so-far variable.
Answer
best = 0; name = ""; for k, v in marks.items(): if v > best: best = v; name = k; then print(name, best). Ali sets best 72, Bea replaces it with 91, Cy (85) does not. Output: Bea 91
Q5 · 5 marks · Short answerHOTS / competency
Give the output of the program and explain the three lines:
d = {"a": 1, "b": 2} e = d e["c"] = 3 f = d.copy() f["d"] = 4 print(d) print(f) print(d is e, {"a": 1, "a": 2})(5 marks)
Hint
Alias versus copy.
Answer
e = d is an alias, so e['c'] = 3 changes d too: {'a': 1, 'b': 2, 'c': 3}. f = d.copy() is a separate dictionary, so f['d'] = 4 changes only f: {'a': 1, 'b': 2, 'c': 3, 'd': 4}. d is e is True. In {'a': 1, 'a': 2} the later value wins: {'a': 2}. Last line output: True {'a': 2}
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.Can keys repeat in a dictionary?Show answer
No; keys are unique, and a repeated key keeps the last value.
2.What does get() return for a missing key?Show answer
None, or the default you give.
3.What does the in operator test in a dictionary?Show answer
The keys.
4.Which method gives (key, value) pairs?Show answer
items().
5.What does setdefault(k, v) do if k exists?Show answer
Returns the existing value without changing it.
Dictionaries: Keys, Values, get, items, update and Counting Characters (CBSE 11): frequently asked questions
Is a dictionary ordered?
From Python 3.7 it keeps the order of insertion; it is still looked up by key, not by position.
How do I count characters with a dictionary?
Loop over the string and use freq[ch] = freq.get(ch, 0) + 1.
What does popitem() do?
It removes and returns the last pair added as a (key, value) tuple.