Dictionaries in Python are one of the most powerful and flexible data types. They are used to store data in the form of key-value pairs, allowing quick access to information based on a unique key.
This beginner-friendly guide will walk you through the basics of how to create dictionaries, how key-value pairs work, and how to access values using keys — all with simple and easy-to-follow examples.
1. What is a Dictionary?
A dictionary is a collection of unordered, mutable key-value pairs. Each key in the dictionary must be unique and immutable (like a string or a number), and it maps to a value that can be of any data type.
# Example 1: Creating a dictionary
person = {
"name": "Rahul",
"age": 28,
"city": "Mumbai"
}
print(person)
2. Accessing Values with Keys
To retrieve a value from the dictionary, you use the key inside square brackets []
or the get()
method.
# Example 2: Accessing values
print(person["name"]) # Rahul
print(person["age"]) # 28
# Example 3: Using get() method (safer)
print(person.get("city")) # Mumbai
print(person.get("email")) # None (instead of error)
3. Adding New Key-Value Pairs
You can add new pairs simply by assigning a value to a new key.
# Example 4: Adding a new key
person["email"] = "rahul@example.com"
print(person)
4. Updating Existing Values
To update a value, use the key and assign a new value to it.
# Example 5: Updating a value
person["city"] = "Delhi"
print(person)
5. Keys Must Be Unique
Keys in a dictionary must be unique. If you define the same key more than once, the last assignment will overwrite the previous one.
# Example 6: Duplicate keys
data = {
"id": 101,
"id": 102
}
print(data) # {'id': 102}
6. Dictionary with Different Data Types
Values in a dictionary can be any data type — strings, numbers, lists, even other dictionaries.
# Example 7
info = {
"name": "Anu",
"scores": [85, 90, 88],
"details": {
"grade": "A",
"active": True
}
}
print(info["scores"])
print(info["details"]["grade"])
7. Empty Dictionary
You can create an empty dictionary and add values later.
# Example 8
user = {}
user["username"] = "user123"
user["status"] = "active"
print(user)
8. Dictionary Length
Use len()
to get the number of key-value pairs in the dictionary.
# Example 9
print(len(person)) # Number of key-value pairs
Best Practices for Working with Dictionaries
- Use descriptive keys that make your data readable.
- Use
get()
when accessing values if you're unsure whether the key exists. - Use dictionaries when you need a logical association between a key and its value.
Common Use Cases
- Storing user profiles or settings
- Mapping names to scores or IDs
- Representing structured data (e.g., from JSON)
Summary
- Dictionaries store data using key-value pairs.
- Keys must be unique and immutable, values can be of any type.
- You can access, add, and update data using keys.
- Dictionaries are ideal for quick lookups and storing structured information.
Final Thoughts
Once you're comfortable creating and accessing key-value pairs, you'll find dictionaries to be one of the most useful tools in your Python toolkit. They are efficient, flexible, and perfect for organizing data clearly and logically.