Python – Access Dictionary Items

1. What Does “Access Dictionary Items” Mean?

Accessing dictionary items means getting values from a dictionary using keys.

Remember:
👉 Dictionaries use keys, not index numbers.

2. Access Item Using Key Name

The easiest way is using the key inside square brackets.

Example

student = {
"name": "Amit",
"age": 20,
"city": "Delhi"
}
print(student[“name”])
print(student[“age”])

Output:

Amit
20

3. Access Item Using get() Method

get() is a safe way to access values.

Example

print(student.get("city"))
print(student.get("marks"))

Output:

Delhi
None

👉 No error if key is missing.

4. Difference Between [] and get()

Method Missing Key
student["key"] Error
student.get("key") Returns None

5. Access All Keys

Example

print(student.keys())

6. Access All Values

Example

print(student.values())

7. Access All Key-Value Pairs

Example

print(student.items())

8. Loop Through Dictionary Items

Loop Keys

for key in student:
print(key)

Loop Values

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

Loop Key and Value

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

9. Check if Key Exists

Example

if "name" in student:
print("Name is present")

10. Access Nested Dictionary Item

Example

data = {
"student": {
"name": "Amit",
"age": 20
}
}
print(data[“student”][“name”])

11. Common Beginner Mistakes

❌ Using Index Instead of Key

print(student[0])

❌ Error.

❌ Accessing Missing Key Directly

print(student["phone"])

✔ Use:

print(student.get("phone"))

12. Simple Practice Examples

Example 1: Access Product Price

product = {
"name": "Mobile",
"price": 15000
}
print(product[“price”])

Example 2: Safe Access

print(product.get("brand"))

Example 3: Loop Dictionary

for k, v in product.items():
print(k, v)

13. Summary (Access Dictionary Items)

✔ Use key to access values
get() is safer than []
✔ Use loops to read all items
✔ Keys must exist for []
✔ Very useful in real programs

📘 Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Change Dictionary Items

  • Add Dictionary Items

  • Remove Dictionary Items

  • Dictionary Methods

  • Dictionary Exercises

Just tell me 😊

You may also like...