Python Dictionary Practice Questions with Solutions

Dictionaries store data as key-value pairs in Python. They are useful for organizing, retrieving, and updating data efficiently. In this Python Dictionary Practice Questions with Solutions set, you’ll learn dictionary creation, accessing values, updating items, removing elements, and common dictionary methods through beginner-friendly practice questions.


1. Python Program to Create and Print a Dictionary

Difficulty: Foundation

Problem Statement

Write a Python program to create a dictionary containing a student’s name, age, and course, then print it.

Python Solution

student = {
    "name": "John",
    "age": 20,
    "course": "Python"
}

print(student)

Sample Output

{'name': 'John', 'age': 20, 'course': 'Python'}

Explanation

A dictionary stores data in key-value pairs using curly braces {}.

Concepts Covered

  • Dictionary Creation
  • Key-Value Pairs

2. Python Program to Access a Dictionary Value

Difficulty: Foundation

Problem Statement

Write a Python program to print the value of the "course" key.

Python Solution

student = {
    "name": "John",
    "age": 20,
    "course": "Python"
}

print(student["course"])

Sample Output

Python

Explanation

Values can be accessed using their corresponding keys.

Concepts Covered

  • Dictionary Access

3. Python Program to Add a New Key-Value Pair

Difficulty: Foundation

Problem Statement

Write a Python program to add a new key "city" with the value "Delhi" to a dictionary.

Python Solution

student = {
    "name": "John",
    "age": 20
}

student["city"] = "Delhi"

print(student)

Sample Output

{'name': 'John', 'age': 20, 'city': 'Delhi'}

Explanation

Assigning a new key automatically adds it to the dictionary.

Concepts Covered

  • Adding Items

4. Python Program to Update a Dictionary Value

Difficulty: Foundation

Problem Statement

Write a Python program to update the age of a student from 20 to 21.

Python Solution

student = {
    "name": "John",
    "age": 20
}

student["age"] = 21

print(student)

Sample Output

{'name': 'John', 'age': 21}

Explanation

Assigning a new value to an existing key updates that value.

Concepts Covered

  • Updating Dictionary Values

5. Python Program to Remove an Item from a Dictionary

Difficulty: Practice

Problem Statement

Write a Python program to remove the "age" key from a dictionary.

Python Solution

student = {
    "name": "John",
    "age": 20,
    "course": "Python"
}

student.pop("age")

print(student)

Sample Output

{'name': 'John', 'course': 'Python'}

Explanation

The pop() method removes the specified key and its value.

Concepts Covered

  • pop()

6. Python Program to Print All Dictionary Keys

Difficulty: Practice

Problem Statement

Write a Python program to print all keys of a dictionary.

Python Solution

student = {
    "name": "John",
    "age": 20,
    "course": "Python"
}

print(student.keys())

Sample Output

dict_keys(['name', 'age', 'course'])

Explanation

The keys() method returns all dictionary keys.

Concepts Covered

  • keys()

7. Python Program to Print All Dictionary Values

Difficulty: Practice

Problem Statement

Write a Python program to print all values of a dictionary.

Python Solution

student = {
    "name": "John",
    "age": 20,
    "course": "Python"
}

print(student.values())

Sample Output

dict_values(['John', 20, 'Python'])

Explanation

The values() method returns all values stored in the dictionary.

Concepts Covered

  • values()

8. Python Program to Print All Key-Value Pairs

Difficulty: Challenge

Problem Statement

Write a Python program to print all key-value pairs in a dictionary.

Python Solution

student = {
    "name": "John",
    "age": 20,
    "course": "Python"
}

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

Sample Output

name : John
age : 20
course : Python

Explanation

The items() method returns both keys and values together.

Concepts Covered

  • items()
  • for Loop

9. Python Program to Check Whether a Key Exists

Difficulty: Challenge

Problem Statement

Write a Python program to check whether the key "course" exists in a dictionary.

Python Solution

student = {
    "name": "John",
    "age": 20,
    "course": "Python"
}

if "course" in student:
    print("Key Found")
else:
    print("Key Not Found")

Sample Output

Key Found

Explanation

The in operator checks whether a key exists in the dictionary.

Concepts Covered

  • Membership Operator

10. Python Program to Count the Total Number of Key-Value Pairs

Difficulty: Challenge

Problem Statement

Write a Python program to count the total number of key-value pairs in a dictionary.

Python Solution

student = {
    "name": "John",
    "age": 20,
    "course": "Python",
    "city": "Delhi"
}

print("Total Items:", len(student))

Sample Output

Total Items: 4

Explanation

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

Concepts Covered

  • len()
  • Dictionary Size

Chapter Summary

After completing this chapter, you have learned:

  • Dictionary creation
  • Accessing values
  • Adding key-value pairs
  • Updating dictionary values
  • Removing items using pop()
  • keys()
  • values()
  • items()
  • Checking whether a key exists
  • Counting dictionary items

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top