Python Dictionaries


dictionary is a collection of key-value pairs.
It is unordered, changeable, and indexed.


🔹 Create a Dictionary

student = {
    "name": "John",
    "age": 22,
    "course": "Python"
}
print(student)

🔹 Access Items

print(student["name"])         # John
print(student.get("age"))      # 22

🔹 Change Values

student["age"] = 23

🔹 Loop Through Dictionary

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

Or use:

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

🔹 Check If Key Exists

if "name" in student:
    print("Yes")

🔹 Dictionary Length

print(len(student))  # 3

🔹 Add Items

student["grade"] = "A"

🔹 Remove Items

student.pop("course")
del student["age"]
student.clear()  # Removes all items

🔹 Copy Dictionary

new_dict = student.copy()

🔹 Nested Dictionaries

students = {
    "student1": {"name": "John", "age": 22},
    "student2": {"name": "Alice", "age": 21}
}

🧪 Try It Yourself

Create a dictionary, update values, loop through items, and practice nested structures.


Practice Questions

Q1. Write a Python program to create a dictionary with keys: "name", "age", and "city".

Q2. Write a Python program to access and print the value of "age" from the dictionary.

Q3. Write a Python program to change the value of "city" to "Delhi".

Q4. Write a Python program to add a new key "country" with the value "India" to the dictionary.

Q5. Write a Python program to remove the key "age" from the dictionary using pop() or del.

Q6. Write a Python program to check if the key "name" exists in the dictionary.

Q7. Write a Python program to print all key-value pairs in the dictionary using a for loop.

Q8. Write a Python program to copy one dictionary into another using the copy() method.

Q9. Write a Python program to create a dictionary with 3 student records, where each student is stored as a nested dictionary.

Q10. Write a Python program to clear all items from a dictionary using the clear() method.


Python Dictionaries Quiz

Q1: What is a dictionary in Python?

A. Ordered key-value pairs
B. Unordered key-value pairs
C. List of values
D. Tuple of keys

Q2: Which symbol is used to define a dictionary?

A. []
B. {}
C. ()
D. <>

Q3: How do you access the value of a key?

A. dict.key
B. dict[key]
C. dict.getkey
D. dict.value

Q4: What does dict.get("age") do?

A. Adds age
B. Removes age
C. Returns the value of "age"
D. Updates age

Q5: How to remove a key from a dictionary?

A. delete key
B. dict.remove(key)
C. dict.pop("key")
D. remove(dict.key)

Q6: Can a dictionary have duplicate keys?

A. Yes
B. No
C. Only if values are different
D. Only in Python 2

Q7: Which method gives all keys in the dictionary?

A. dict.values()
B. dict.items()
C. dict.keys()
D. dict.list()

Q8: What does dict.clear() do?

A. Removes one item
B. Deletes the dict
C. Removes all items
D. Copies the dict

Q9: Can dictionary values be lists or other dictionaries?

A. No
B. Yes
C. Only lists are allowed
D. Only strings and numbers are allowed

Q10: Which method returns both keys and values?

A. dict.all()
B. dict.kv()
C. dict.items()
D. dict.pairs()

Go Back Top