Last updated on August 9th, 2026 at 07:36 pm
Dictionary in Python Class 11 Notes
Welcome to the complete Dictionary in Python โ Class 11 Notes. A dictionary is Python’s key-value data structure โ instead of finding items by position (like in a list), you find them by a unique key. It’s one of the most practical and most-asked chapters in CBSE Computer Science / Informatics Practices. These notes cover the complete CBSE syllabus โ accessing items, mutability, traversal, every built-in function/method, and the two suggested programs โ with syntax, examples and outputs.
dict[key] and dict.get(key), and between pop() and popitem(), are the two most repeated trick questions on this chapter. Both are explained below with examples.Quick Overview โ What You Will Learn
| # | Topic | Key Concepts | Exam Weight |
|---|---|---|---|
| 1 | Introduction to Dictionary | Key-value pairs, creation, unique keys | โญโญโญ |
| 2 | Accessing Items | Using [ ] and get() | โญโญโญ |
| 3 | Mutability | Adding a new term, modifying an existing item | โญโญ |
| 4 | Traversing a Dictionary | for loop over keys, values, items | โญโญ |
| 5 | Built-in Functions/Methods | len(), dict(), keys(), values(), items(), get(), update(), del, clear(), fromkeys(), copy(), pop(), popitem(), setdefault(), max(), min(), sorted() | โญโญโญ |
| 6 | Suggested Programs | Character frequency, employee salary dictionary | โญโญโญ |
Section 1 ยท Introduction ยท โญโญโญ
๐ฆ What is a Dictionary in Python?
A dictionary is an unordered, mutable collection of key-value pairs, enclosed in curly braces
{ }. Instead of a numeric index, each value is accessed through its unique key.
Key-Value PairsMutableUnique Keysdict type
print(day)
print(type(day))
<class ‘dict’>
Key Rules for Dictionaries
Keys must be unique and of an immutable type (string, number, or tuple) โ a list or another dictionary can never be used as a key. Values, however, can be any type.
print(student)
# mylist = [1, 2]
# bad_dict = {mylist: “error”} # TypeError: unhashable type: ‘list’
Section 2 ยท Accessing Items ยท โญโญโญ
๐ Accessing Items in a Dictionary Using Keys
Dictionaries have no numeric index โ you access a value by writing its key inside square brackets, or by using the safer
get()method.
print(day[1]) # Monday
print(day.get(1)) # Monday — safer way
print(day[9]) # KeyError: 9
print(day.get(9)) # None — no error!
print(day.get(9, “Not Found”)) # custom default value
Monday
KeyError: 9
None
Not Found
dict[key] raises a KeyError if the key doesn’t exist. dict.get(key) returns None (or a default value you specify) instead of crashing the program โ much safer for real programs.Section 3 ยท Mutability ยท โญโญ
โ๏ธ Mutability of a Dictionary
A dictionary is mutable โ you can add new key-value pairs and modify existing ones after creation, using the same square-bracket syntax.
Adding a New Term
day[4] = “Thursday” # new key — added to dictionary
print(day)
Modifying an Existing Item
day[1] = “Sunday” # existing key — value gets updated
print(day)
dict[key] = value โ if the key already exists, its value is updated; if the key is new, it is added. Python decides automatically.Section 4 ยท Traversal ยท โญโญ
๐ Traversing a Dictionary
You can loop through a dictionary’s keys, values, or key-value pairs together. By default, looping over a dictionary gives you its keys.
# Method 1 — loop gives keys by default
for key in day:
print(key, “-“, day[key])
# Method 2 — using items() for key AND value together
for key, value in day.items():
print(key, “->”, value)
2 – Tuesday
3 – Wednesday
1 -> Monday
2 -> Tuesday
3 -> Wednesday
for key, value in dict.items() is the cleanest and most common way to traverse a dictionary โ memorise this pattern.Section 5 ยท Built-in Functions/Methods ยท โญโญโญ
๐ ๏ธ Built-in Functions and Methods on Dictionaries
The complete CBSE syllabus list:
len(),dict(),keys(),values(),items(),get(),update(),del,clear(),fromkeys(),copy(),pop(),popitem(),setdefault(),max(),min(),sorted().
len() and dict()
print(len(day)) # 2 — number of key-value pairs
pairs = dict([(1, “a”), (2, “b”)]) # list of tuples -> dict
print(pairs)
{1: ‘a’, 2: ‘b’}
keys(), values() and items()
print(day.keys())
print(day.values())
print(day.items())
dict_values([‘Monday’, ‘Tuesday’, ‘Wednesday’])
dict_items([(1, ‘Monday’), (2, ‘Tuesday’), (3, ‘Wednesday’)])
update()
update()merges another dictionary into the current one โ existing keys get their values updated, and new keys get added.
day.update({2: “TUESDAY”, 3: “Wednesday”})
print(day)
del, clear(), pop() and popitem()
Four ways to delete:
delremoves one key;pop(key)removes one key AND returns its value;popitem()removes the last inserted pair;clear()empties the whole dictionary.
del day[1] # removes key 1
print(day)
removed_value = day.pop(2) # removes key 2, returns its value
print(removed_value)
print(day)
last_pair = day.popitem() # removes the last inserted pair
print(last_pair)
print(day)
day.clear() # empties the dictionary
print(day)
Tuesday
{3: ‘Wednesday’}
(3, ‘Wednesday’)
{}
{}
pop(key) removes a specific key you name and returns its value. popitem() takes no key โ it always removes the most recently added key-value pair as a tuple.fromkeys()
Creates a new dictionary from a sequence of keys, all set to the same default value.
new_dict = dict.fromkeys(keys_list, 0)
print(new_dict)
copy()
Returns a separate copy of the dictionary โ changes to the copy do not affect the original.
day_copy = day.copy()
day_copy[3] = “Wednesday”
print(“Original:”, day)
print(“Copy:”, day_copy)
Copy: {1: ‘Monday’, 2: ‘Tuesday’, 3: ‘Wednesday’}
setdefault()
Similar to
get(), but if the key is missing, it also inserts it into the dictionary with the given default value.
print(day.setdefault(2, “Tuesday”)) # key 2 missing -> inserted
print(day)
{1: ‘Monday’, 2: ‘Tuesday’}
max(), min() and sorted() on a Dictionary
When used directly on a dictionary,
max(),min()andsorted()work on its keys by default.
print(max(marks)) # ‘Sam’ — largest key alphabetically
print(min(marks)) # ‘Amit’ — smallest key alphabetically
print(sorted(marks)) # sorted list of keys
# To work on VALUES instead of keys:
print(max(marks, key=marks.get)) # key with the highest value -> ‘Riya’
Amit
[‘Amit’, ‘Riya’, ‘Sam’]
Riya
max(dict) compares keys, not values. To find the key with the highest value (e.g. topper’s name), use max(dict, key=dict.get).๐ Complete Summary Table โ All CBSE Syllabus Dictionary Methods
| Function/Method | What it Does |
|---|---|
len(dict) | Number of key-value pairs |
dict(seq) | Creates a dictionary from key-value pairs |
keys() | Returns all keys |
values() | Returns all values |
items() | Returns all (key, value) pairs |
get(key) | Returns value for key, or None (no KeyError) |
update(dict2) | Merges dict2 into the dictionary |
del dict[key] | Removes one key-value pair |
clear() | Removes all key-value pairs |
fromkeys(seq, val) | New dict with given keys, same default value |
copy() | Returns a separate copy of the dictionary |
pop(key) | Removes given key, returns its value |
popitem() | Removes & returns the last inserted pair |
setdefault(key, val) | get(), but also inserts key if missing |
max(dict) / min(dict) | Largest / smallest key |
sorted(dict) | Sorted list of keys |
Section 6 ยท Suggested Programs ยท โญโญโญ
๐ป Common Programs for CBSE Practicals & Board Exams
Program 1 โ Count Frequency of Characters in a String Using a Dictionary
freq = {}
for ch in text:
if ch in freq:
freq[ch] += 1
else:
freq[ch] = 1
for key, value in freq.items():
print(key, “->”, value)
r -> 2
o -> 1
g -> 2
a -> 1
m -> 2
i -> 1
n -> 1
freq[ch] = freq.get(ch, 0) + 1 โ this avoids the if-else check entirely.Program 2 โ Employee Names and Salary Dictionary
“Amit”: 45000,
“Riya”: 52000,
“Sam”: 38000
}
# Accessing a specific employee’s salary
print(“Riya’s salary:”, employees[“Riya”])
# Traversing and displaying all employees with salary
for name, salary in employees.items():
print(name, “earns Rs.”, salary)
# Finding the highest-paid employee
topper = max(employees, key=employees.get)
print(“Highest paid:”, topper, “-“, employees[topper])
Amit earns Rs. 45000
Riya earns Rs. 52000
Sam earns Rs. 38000
Highest paid: Riya – 52000
Frequently Asked Questions
What is the difference between dict[key] and dict.get(key)?dict[key] raises a KeyError if the key is missing. dict.get(key) returns None (or a chosen default) instead, avoiding a crash.
What is the difference between pop() and popitem()?pop(key) removes a specific key you name and returns its value. popitem() takes no argument and always removes the last inserted key-value pair.
Can dictionary keys be a list?
No. Keys must be immutable (string, number, or tuple). A list is mutable, so it cannot be a key โ Python raises TypeError: unhashable type.
Is a dictionary ordered in Python?
From Python 3.7 onward, dictionaries maintain insertion order as an implementation detail, but conceptually for CBSE purposes a dictionary is treated as an unordered, key-based collection โ items are accessed by key, not by position.
What does setdefault() do differently from get()?get() only reads a value. setdefault() reads the value too, but if the key doesn’t exist, it also inserts that key into the dictionary with the given default.
How do you find the key with the maximum value in a dictionary?
Use max(dict, key=dict.get) โ plain max(dict) only compares the keys, not the values.
Quick Practice Links
๐ Dictionary MCQs Class 11 | Python Compiler | ๐ Practical File | ๐ All Class 11 Notes
Explore More Class 11 CS Notes
๐ Python List | ๐ Python Tuple | ๐ Python String | ๐ง Python Functions
Found an error or a missing example? Drop a comment below โ we fix issues quickly. If these notes helped you, share this page with your classmates!
