Learn Python in 30 Days — Day 12: Dictionaries
Learn Python in 30 Days — Day 12: Dictionaries
Welcome to Day 12 of the Learn Python in 30 Days series!
Yesterday we explored tuples and sets, which let you store ordered and unique data.
Today, we move on to something that will become a cornerstone of your Python projects dictionaries.
Dictionaries are the foundation of nearly every real-world Python application be it configuration files, APIs, game states, and even databases, they all rely on this structure.
Think of them as mini databases in memory, perfect for storing information with named labels instead of numbered positions.
By the end of this lesson you’ll be able to:
-
Create and manipulate dictionaries with confidence
-
Understand how keys and values work under the hood
-
Store nested and complex data structures
-
Combine loops and conditionals to build a Contact Book mini-project
What is a Dictionary?
A dictionary stores data as key → value pairs.
Instead of lists, which rely on numeric positions, dictionaries let you store data under descriptive labels:
Access values by their keys:
They’re also known as hash maps in other languages, and they’re fast. Python dictionaries use hash tables internally, giving you near-instant lookups even in large data sets.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Creating Dictionaries
There are a few ways to create dictionaries.
Explanation:
-
{}creates a dictionary -
Each key is separated by a colon from its value
-
Commas separate items
-
Duplicate keys will overwrite previous ones
Expected Output:
Keys must be unique and immutable (e.g. strings, numbers, tuples). If you repeat a key, the last one wins.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Accessing and Reading Data
You can access values using their keys, not numbers like in a list.
If the key doesn’t exist, Python will raise an error.
To avoid that, use .get():
.get() safely checks if a key exists and returns a default message if not.
Expected Output:
Why use .get()?
It prevents your program from crashing if the key is missing — very useful when working with user or file data.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Adding and Updating Entries
Dictionaries are flexible — you can add new data or update existing items anytime.
Explanation:
-
Adding is just assigning a new key
-
.update()adds or changes multiple keys at once
Expected Output:
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Removing Items
Dictionary Views (Keys, Values, Items)
You can easily view all keys, values, or both at once.
Expected Output:
These are live views if you change the dictionary, these update automatically.
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Looping Through Dictionaries
Dictionaries are perfect for looping through data.
Explanation:
-
Looping directly gives you the keys
-
.items()lets you unpack both key and value for cleaner loops
Expected Output:
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Checking for Keys
You can check if a key exists before using it.
Explanation:
The in keyword checks for membership quickly — faster than looping through keys manually.
Expected Output:
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Nested and Complex Dictionaries
Dictionaries can hold other dictionaries or lists — this is how you store structured data (like JSON files).
Dictionary inside a Dictionary
List inside a Dictionary
Dictionaries inside a List
Expected Output:
Why use nested dictionaries?
This is the structure used in JSON, the universal data format you’ll soon encounter when working with files and web data.Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Advanced Insight — How Dictionaries Work Internally
Python dictionaries are hash tables.
When you create a key, Python:
-
Computes a unique hash value for that key
-
Maps it to a position in memory
-
Stores or retrieves the value in constant time, O(1)
That’s why dictionary lookups are far faster than scanning through lists.
However, this also means:
-
Keys must be immutable
-
You cannot use lists or other dictionaries as keys
Practical Tip: Dictionary Comprehensions
You can quickly create dictionaries using a short syntax, similar to list comprehensions.
Explanation:
This loops through numbers 0–4, and for each number x, it stores x as the key and x**2 (x squared) as the value.
Expected Output:
Mini Project — Simple Contact Book
Let’s put all this into action with a Contact Book that uses dictionaries as an in-memory database.
Concepts used:
-
Dictionaries as data stores
-
.get()and.pop()for safe access -
.items()for looping -
Lists, conditionals, and formatted output
This is your first small “application” a working, stateful tool built entirely from what you’ve learned so far.
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Wrap-Up
You now understand one of Python’s most versatile and powerful data types.
Dictionaries = Fast, structured, and human-readable data.
They’re the backbone of almost everything you’ll do from now on — from managing user profiles and app settings to reading JSON from APIs.
Next up — Day 13 – Nested Structures & Looping
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty











Comments
Post a Comment