HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Programming
  2. /Python Essentials
  3. /Dictionaries

Dictionaries

Python Programming · High School (9–10) · 2 courses, 27 worked problems, 37 practice questions

A dictionary stores values under keys, as a glossary stores meanings under words. In Python you write marks = {"Asha": 72, "Ben": 65}, and marks["Asha"] gives 72. Keys must be unique and are often strings. Values can be anything, even lists or other dictionaries. You can add a pair, change a value, or remove a pair with del. The methods .keys(), .values() and .items() let a loop visit every pair. Looking up a key is fast, so dictionaries suit word counts, phone books and tables of elements.

With practice you will be able to

  • Create a dictionary and look up a value by its key
  • Add, change and remove key-value pairs
  • Loop over a dictionary with .items() to count or total values

On the syllabus of

  • CBSE / State Class 12
  • Python Certification
  • A-Level

Lessons

  • Three Essentials ProjectsCan you crack a Caesar code without knowing the shift?High School (9–10)
  • Dictionaries: Look Things UpHow do you count every letter in banana? What if the key you ask for is not there?High School (9–10)

Practice questions

Choose an answer, then check it. Each one shows a full explanation.

A price list is kept in a dictionary. The program changes one price, adds an item, and removes another. prices = {"tea": 20, "bun": 15} prices["tea"] = 25 prices["jam"] = prices.get("bun", 0) + 5 del prices["bun"] print(list(prices.keys()), sum(prices.values())) What does the program print?

A theatre stores its seats in a dictionary. Each key is a tuple of a row number and a seat letter. seats = {(1, "A"): "Ann", (1, "B"): "Raj", (2, "A"): "Mo"} row1 = [] for key in seats: if key[0] == 1: row1.append(key[1] + ":" + seats[key]) print(row1, (2, "A") in seats, ("A", 2) in seats) What does the program print?

A program should group words by their first letter, in a dictionary of lists. It stops with KeyError. The lines are numbered from 1. groups = {} for word in ["ant", "bee", "ape", "bat"]: k = word[0] groups[k].append(word) print(groups) Which changes make the program print {'a': ['ant', 'ape'], 'b': ['bee', 'bat']}? Choose two.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

A loop doubles every value of a dictionary, taking the keys in sorted order. Then the program looks at the dictionary in two ways. scores = {"b": 3, "a": 5, "c": 1} for k in sorted(scores): scores[k] = scores[k] * 2 print(list(scores.items())[0], sorted(scores.values())) What does the program print?

A function adds up points for the letters of a word. Letters that are not in the dictionary get a default value. def score(word, values): total = 0 for ch in word: total += values.get(ch, 1) return total pts = {"a": 2, "z": 10} print(score("pizza", pts), len(pts)) What does the program print?

A program updates a dictionary and asks it four questions. d = {"a": 1, "b": 2} d.update({"b": 20, "c": 30}) print(d.get("z", d["a"]), "20" in d, 20 in d.values(), len(d)) What does the program print?

A program counts the letters of a word in a dictionary, and then asks the dictionary three things. word = "abca" count = {} for ch in word: count[ch] = count.get(ch, 0) + 1 print(count, len(count), count.get("z")) What does the program print?

contacts = {"ravi": "98765"}. What does contacts.get("Ravi".lower()) give?

A message was coded with a shift of 19. Which shift, from 0 to 25, decodes it with the same function?

ranked = sorted([("Asha", 72), ("Ravi", 85), ("Meera", 64)], key=lambda p: -p[1]). What is ranked[0][0]?

All 37 practice questions on Dictionaries →

Worked problems

Each one has a hint, the key idea and a full worked solution.

  • Write three small functions square(n), double(n) and negate(n), and a dictionary operations that…Medium
  • Choose a project: a program that keeps the scores of a quiz game and shows who is winning…Easy
  • Write a function above_mean(marks) for a dictionary of name and mark…Medium
  • Say whether a list or a dictionary is the better choice for each, and why…Easy
  • Write a program that counts how many times each letter appears in the word mississippi, and shows…Easy
  • For this dictionary, write the expression for each answer…Medium
  • Group these words by their length, in a dictionary where the key is the length and the value is the…Medium
  • Write add(name, number) for the contact book…Easy
  • The book has asha and ravi. What happens with contacts["zoya"]? …Easy
  • The book is {"meera": "99887", "asha": "98765", "ravi": "91234", "kabir": "90000"}…Medium
  • Count the letters of balloon with a dictionary…Medium
  • In a long English secret, the most common letter is x…Medium

All 27 worked problems on Dictionaries →

Python labs

  • Contact book tools
  • Guess the shift
  • Marks report tools
  • Run the commands
  • Project: the command menu
  • Plan and test: a unit converter

Related topics

  • Python Essentials
  • Tuples and Sets
  • Errors and Exceptions
  • Modules and Libraries
  • Searching and Sorting
  • Recursion
  • Text Processing
  • Essentials Projects