Informatics Practices

Python Dictionary | Creating, Initializing, Traversing, Updating, Deleting and Dictionary Methods | Complete Notes | CBSE Class 11 Informatics Practices (2026–27)

Class 11 · Informatics Practices

Python Dictionary | Creating, Initializing, Traversing, Updating, Deleting and Dictionary Methods

A list stores data using numerical indexes, but in many real-life applications, information is stored using a unique identifier. For example, a student's roll number is associated with a student's name, an employee ID is associated with employee details, and a country's name is associated with its capital. Python provides the Dictionary data type to store such information efficiently.

A dictionary stores data in the form of key-value pairs. Each key uniquely identifies a value, making dictionaries fast and efficient for searching, updating, and managing data.


Learning Outcomes

After studying this chapter, you will be able to:

  • Understand the concept of dictionaries.
  • Create and initialize dictionaries.
  • Access dictionary elements.
  • Traverse a dictionary.
  • Update and delete dictionary elements.
  • Use important dictionary methods.

What is a Dictionary?

Definition

A dictionary is an unordered collection of data stored as key-value pairs. Every key in a dictionary must be unique and is associated with a corresponding value.

Example

student = {
    "Roll No": 101,
    "Name": "Aarav",
    "Class": "XI",
    "Marks": 92
}

print(student)

Why Do We Need Dictionaries?

Without Dictionary

roll = 101
name = "Aarav"
marks = 92

Using Dictionary

student = {
    "Roll No":101,
    "Name":"Aarav",
    "Marks":92
}

A dictionary keeps related information together and allows quick access using keys instead of index numbers.


Characteristics of Dictionaries

  • Store data in key-value pairs.
  • Keys must be unique.
  • Values may be duplicated.
  • Dictionaries are mutable.
  • Elements are accessed using keys instead of indexes.
  • Different data types can be stored as values.

Creating a Dictionary

A dictionary is created using curly braces { }.

Syntax

dictionary_name = {
    key1:value1,
    key2:value2
}

Example

employee = {
    "ID":1001,
    "Name":"Riya",
    "Salary":45000
}

Creating an Empty Dictionary

student = {}

Using the dict() Function

Definition

The dict() function creates a dictionary from key-value pairs or another mapping object.

Example

student = dict(Name="Diya", Age=16, City="Jaipur")

print(student)

Output

{'Name': 'Diya', 'Age': 16, 'City': 'Jaipur'}

Initializing a Dictionary

Initializing means assigning key-value pairs at the time of creating a dictionary.

book = {
    "Title":"Python",
    "Author":"ABC",
    "Price":650
}

Accessing Dictionary Elements

Dictionary values are accessed using their corresponding keys.

Syntax

dictionary_name[key]

Example

student = {
    "Name":"Kabir",
    "Marks":95
}

print(student["Name"])
print(student["Marks"])

Output

Kabir
95

Traversing a Dictionary

Traversing means accessing every key-value pair one by one.

Example

student = {
    "Name":"Riya",
    "Class":"XI",
    "Marks":91
}

for key in student:
    print(key, ":", student[key])

Output

Name : Riya
Class : XI
Marks : 91

Finding the Length of a Dictionary

Definition

The len() function returns the total number of key-value pairs present in a dictionary.

Example

student = {
    "Name":"Diya",
    "Age":16,
    "City":"Jaipur"
}

print(len(student))

Output

3

Real-Life Applications of Dictionaries

Application Dictionary Example
School ERP Roll Number → Student Details
Library Book ID → Book Information
Hospital Patient ID → Patient Record
Bank Account Number → Customer Details
Online Shopping Product ID → Product Information


Updating Dictionary Elements

Definition

Since dictionaries are mutable, their values can be modified, and new key-value pairs can be added after the dictionary has been created.

Updating an Existing Value

student = {
    "Name":"Aarav",
    "Marks":85
}

student["Marks"] = 92

print(student)

Output

{'Name': 'Aarav', 'Marks': 92}

Adding a New Key-Value Pair

student = {
    "Name":"Aarav",
    "Marks":85
}

student["Class"] = "XI"

print(student)

Output

{'Name': 'Aarav', 'Marks': 85, 'Class': 'XI'}

Deleting Dictionary Elements

Dictionary elements can be deleted using the del statement or by clearing the entire dictionary.


The del Statement

Definition

The del statement removes a specified key-value pair from the dictionary.

Syntax

del dictionary_name[key]

Example

student = {
    "Name":"Diya",
    "Class":"XI",
    "Marks":91
}

del student["Class"]

print(student)

Output

{'Name': 'Diya', 'Marks': 91}

The clear() Method

Definition

The clear() method removes all key-value pairs from a dictionary, making it empty.

Syntax

dictionary_name.clear()

Example

student = {
    "Name":"Kabir",
    "Marks":95
}

student.clear()

print(student)

Output

{}

The keys() Method

Definition

The keys() method returns a view object containing all the keys present in the dictionary.

Syntax

dictionary_name.keys()

Example

student = {
    "Name":"Riya",
    "Class":"XI",
    "Marks":90
}

print(student.keys())

Output

dict_keys(['Name', 'Class', 'Marks'])

The values() Method

Definition

The values() method returns a view object containing all the values stored in the dictionary.

Syntax

dictionary_name.values()

Example

student = {
    "Name":"Riya",
    "Class":"XI",
    "Marks":90
}

print(student.values())

Output

dict_values(['Riya', 'XI', 90])

The items() Method

Definition

The items() method returns all key-value pairs as tuple objects.

Syntax

dictionary_name.items()

Example

student = {
    "Name":"Kabir",
    "Marks":95
}

print(student.items())

Output

dict_items([('Name', 'Kabir'), ('Marks', 95)])

Traversing Using items()

student = {
    "Name":"Aarav",
    "Class":"XI",
    "Marks":94
}

for key, value in student.items():
    print(key, ":", value)

Output

Name : Aarav
Class : XI
Marks : 94

The update() Method

Definition

The update() method adds new key-value pairs or updates existing values in a dictionary.

Syntax

dictionary_name.update(other_dictionary)

Example 1: Updating a Value

student = {
    "Name":"Riya",
    "Marks":82
}

student.update({"Marks":90})

print(student)

Output

{'Name': 'Riya', 'Marks': 90}

Example 2: Adding New Data

student = {
    "Name":"Riya",
    "Marks":82
}

student.update({"City":"Jaipur"})

print(student)

Output

{'Name': 'Riya', 'Marks': 82, 'City': 'Jaipur'}

Comparison of Dictionary Methods

Method / Statement Purpose
dict() Creates a dictionary.
len() Returns the number of key-value pairs.
keys() Returns all keys.
values() Returns all values.
items() Returns all key-value pairs.
update() Adds or updates key-value pairs.
clear() Removes all elements.
del Deletes a specific key-value pair.

Difference Between List and Dictionary

List Dictionary
Stores elements in sequence. Stores key-value pairs.
Uses indexes. Uses keys.
Elements are accessed by position. Values are accessed by keys.
Duplicate elements are allowed. Keys must be unique.
Created using [ ] Created using { }


Real-Life Applications of Dictionaries

Application How Dictionary is Used
School ERP Roll Number → Student Details
Hospital Management System Patient ID → Medical Record
Banking System Account Number → Customer Information
Library Management Book ID → Book Details
Online Shopping Product ID → Product Information
Weather Application City Name → Temperature

Solved Program 1: Display Student Details

student = {
    "Roll No":101,
    "Name":"Aarav",
    "Marks":95
}

for key, value in student.items():
    print(key, ":", value)

Output

Roll No : 101
Name : Aarav
Marks : 95

Solved Program 2: Add a New Key

student = {
    "Name":"Diya",
    "Marks":91
}

student["City"] = "Jaipur"

print(student)

Output

{'Name': 'Diya', 'Marks': 91, 'City': 'Jaipur'}

Solved Program 3: Update Marks

student = {
    "Name":"Kabir",
    "Marks":82
}

student["Marks"] = 94

print(student)

Output

{'Name': 'Kabir', 'Marks': 94}

Solved Program 4: Delete a Key

student = {
    "Name":"Riya",
    "Class":"XI",
    "Marks":89
}

del student["Class"]

print(student)

Output

{'Name': 'Riya', 'Marks': 89}

Solved Program 5: Display Only Keys

book = {
    "Title":"Python",
    "Author":"ABC",
    "Price":650
}

for key in book.keys():
    print(key)

Output

Title
Author
Price

Solved Program 6: Display Only Values

book = {
    "Title":"Python",
    "Author":"ABC",
    "Price":650
}

for value in book.values():
    print(value)

Output

Python
ABC
650

Solved Program 7: Count the Number of Entries

employee = {
    "ID":1001,
    "Name":"Neha",
    "Department":"IT",
    "Salary":55000
}

print(len(employee))

Output

4

Common Errors

Mistake Correct Practice
Using duplicate keys. Always use unique keys in a dictionary.
Accessing a key that does not exist. Ensure the key exists before accessing its value.
Confusing keys with values. Remember that keys identify values.
Using indexes instead of keys. Dictionaries use keys, not numeric indexes.
Deleting a non-existing key using del. Check that the key exists before deleting it.

Interview Corner

Q. What is the main difference between a list and a dictionary?

Answer: A list stores elements in sequence and accesses them using indexes, whereas a dictionary stores data as key-value pairs and accesses values using unique keys.


Competency-Based Questions

  1. A school ERP stores student information using roll numbers as unique identifiers. Which Python data type is most suitable and why?
  2. An online shopping application stores product IDs along with product names and prices. Explain why a dictionary is a better choice than a list.
  3. A librarian wants to update the price of a book stored in a dictionary. Which statement or method can be used?
  4. A banking application needs to display all account numbers stored in a dictionary. Which dictionary method should be used?
  5. A hospital management system wants to remove the details of a discharged patient from a dictionary. Which Python statement is appropriate?

Multiple Choice Questions

  1. Which symbol is used to create a dictionary?
    • (a) [ ]
    • (b) { }
    • (c) ( )
    • (d) < >
  2. Dictionary elements are accessed using:
    • (a) Index
    • (b) Key
    • (c) Position
    • (d) Loop Counter
  3. Which method returns all keys of a dictionary?
    • (a) values()
    • (b) keys()
    • (c) items()
    • (d) update()
  4. Which method removes all elements from a dictionary?
    • (a) remove()
    • (b) pop()
    • (c) clear()
    • (d) delete()
  5. Which statement deletes a specific key-value pair?
    • (a) clear()
    • (b) remove()
    • (c) del
    • (d) erase()

Quick Revision

Concept Remember
Dictionary Stores data as key-value pairs.
dict() Creates a dictionary.
len() Returns the number of key-value pairs.
keys() Returns all keys.
values() Returns all values.
items() Returns key-value pairs.
update() Adds or updates data.
clear() Removes all key-value pairs.
del Deletes a specified key-value pair.

Important Points to Remember

  • Dictionaries store data as key-value pairs.
  • Keys must always be unique.
  • Values may be duplicated.
  • Dictionaries are mutable.
  • Elements are accessed using keys, not indexes.
  • dict() creates a dictionary.
  • keys(), values(), and items() return view objects.
  • update() adds new data or modifies existing data.
  • clear() removes all entries from a dictionary.
  • del deletes a specific key-value pair.

CBSE Exam Tips

  • Understand the difference between lists and dictionaries.
  • Practice creating dictionaries using both { } and dict().
  • Remember the purpose of keys(), values(), items(), update(), and clear().
  • Practice traversing dictionaries using both for key in dictionary and for key, value in dictionary.items().
  • Be careful while accessing or deleting keys that may not exist.
  • Practice output-based questions involving dictionary methods.

Summary

A dictionary is a powerful built-in Python data structure that stores information as key-value pairs. It provides an efficient way to organize and retrieve data using unique keys instead of numeric indexes. Python offers several built-in methods such as keys(), values(), items(), update(), and clear() to simplify dictionary operations. Dictionaries are widely used in real-world applications such as school management systems, banking, hospitals, libraries, and online shopping platforms because they provide fast and organized access to information.