Python Coding Day 18 | Understanding Python Dictionaries and Nesting (Beginner-Friendly Guide)
📘 Understanding Python Dictionaries and Nesting (Beginner-Friendly Guide)
Watch the lesson tutorial 🔻
Python dictionaries are one of the most powerful data structures in Python. They allow you to store information in key–value pairs, making your programs easy to read and manage.
In this article, we’ll walk through:
What dictionaries are
How to retrieve, add, and edit items
How to loop through dictionaries
What nesting means (lists inside dictionaries, dictionaries inside lists, etc.)
Examples explained clearly and simply
🔥 What Is a Dictionary in Python?
A Python dictionary stores data as key–value pairs, like a real dictionary where you search a word (key) to find its meaning (value).
programming_dictionary = {
"Bug": "An error in a program that prevents the program from running as expected.",
"Function": "A piece of code that you can easily call over and over again.",
}
✔ Explanation:
"Bug"is a key"An error in a program..."is the valueDictionaries use
{ }curly brackets.
🔍 Retrieving Items From a Dictionary
print(programming_dictionary["Function"])
✔ When you use a key inside square brackets, Python returns the matching value.
➕ Adding New Items
programming_dictionary["Loop"] = "The action of doing something over and over again."
✔ This adds a new key–value pair to the dictionary.
🧹 Creating an Empty Dictionary
empty_dictionary = {}
✔ Useful when you want to start fresh and add data later.
✏ Editing an Existing Key
programming_dictionary["Bug"] = "A moth in your computer."
✔ This updates the value linked to the key "Bug".
🔄 Looping Through a Dictionary
for key in programming_dictionary:
print(key)
print(programming_dictionary[key])
✔ Explanation:
The loop runs through each key in the dictionary.
First it prints the key.
Then it prints the value using
programming_dictionary[key].
🧩 Understanding Nesting in Python
Python allows us to combine data structures inside each other.
This is called Nesting.
Let’s look at the examples one by one.
1️⃣ Nesting a Dictionary in a Dictionary
capitals = {
"France": "Paris",
"Germany": "Berlin",
}
✔ A simple dictionary with countries as keys and capitals as values.
2️⃣ Nesting a List Inside a Dictionary
travel_log = {
"France": ["Paris", "Lille", "Dijon"],
"Germany": ["Berlin", "Hamburg", "Stuttgart"],
}
✔ Explanation:
"France"→ key["Paris", "Lille", "Dijon"]→ list of citiesUseful for storing multiple pieces of information under one key.
3️⃣ Nesting a Dictionary Inside a Dictionary
travel_log = {
"France": {"cities_visited": ["Paris", "Lille", "Dijon"], "total_visits": 12},
"Germany": {"cities_visited": ["Berlin", "Hamburg", "Stuttgart"], "total_visits": 5},
}
✔ Explanation:
Each country contains:
a list (
cities_visited)a number (
total_visits)
This structure is detailed and perfect for real data storage.
4️⃣ Nesting Dictionaries Inside a List
travel_log = [
{
"country": "France",
"cities_visited": ["Paris", "Lille", "Dijon"],
"total_visits": 12,
},
{
"country": "Germany",
"cities_visited": ["Berlin", "Hamburg", "Stuttgart"],
"total_visits": 5,
},
]
✔ Explanation:
Each
{ ... }block is a dictionary.All dictionaries are stored inside a list.
This format is common in APIs, JSON data, and real-world applications.
🎯 Final Summary
Python dictionaries allow you to:
Store data in key–value format
Access items quickly
Add, remove, and update values
Loop through keys and values
Combine lists and dictionaries using nesting
These techniques are essential for building real-world applications like:
✔ data processing
✔ storing user information
✔ working with APIs
✔ organizing large datasets

Comments
Post a Comment