What Is a Dictionary in Python?
A dictionary in Python is a way to store related data using keys and values.
This is useful when your data has labels. For example, a person can have a name, age, and city. Instead of storing those values by position, a dictionary stores them by name.
This page explains the basic idea of a dictionary, what it stores, and when to use one.
Quick example
person = {
"name": "Alice",
"age": 25,
"city": "Paris"
}
print(person["name"])
print(person["age"])
Output:
Alice
25
Use a dictionary when you want to store related values using names called keys.
Simple definition
A dictionary is a Python data type that stores data as key-value pairs.
- A key is the name you use to find a value
- A value is the data stored for that key
- Dictionaries are useful when data has labels like
name,age, orprice
You will often see dictionaries used for:
- user information
- settings
- product details
- lookup data
What key-value pairs look like
Here is a simple dictionary:
person = {"name": "Alice"}
In this example:
"name"is the key"Alice"is the value
A dictionary connects the key to the value.
Some important points:
- Each key points to one value
- Keys in the same dictionary should be unique
- Keys are often strings, but they do not have to be
For example, number keys also work:
scores = {
1: "low",
2: "medium",
3: "high"
}
print(scores[2])
Output:
medium
Why dictionaries are useful
Dictionaries are useful because they make data easier to read and understand.
Instead of remembering that person[0] is a name and person[1] is an age, you can use clear labels like:
person["name"]person["age"]
This makes dictionaries a good choice for:
- storing records
- saving settings
- mapping one value to another
- looking up data by name or ID
They are often easier to read than long lists when the data is labeled.
Basic dictionary example
You create a dictionary with curly braces {}.
- Separate each key-value pair with a comma
- Use a colon
:between each key and value - Access a value with square brackets and the key
Example:
book = {
"title": "Python Basics",
"pages": 200,
"price": 19.99
}
print(book["title"])
print(book["price"])
Output:
Python Basics
19.99
In this example:
"title"is a key"Python Basics"is its valuebook["title"]gets the value for that key
If you want to learn the syntax in more detail, see creating a dictionary in Python.
Important beginner facts
There are a few important things beginners should know about dictionaries.
- Dictionaries are mutable, which means you can change them after creating them
- You can add new key-value pairs later
- You can update existing values
- Dictionary keys are not based on numeric positions like list indexes
Example:
person = {"name": "Alice", "age": 25}
person["age"] = 26
person["city"] = "Paris"
print(person)
Output:
{'name': 'Alice', 'age': 26, 'city': 'Paris'}
This means dictionaries are flexible. You can start with a small set of data and change it as needed.
Dictionary vs list
A dictionary is not the same as a list.
Use a list when:
- order matters
- positions matter
- you want to access items by index like
items[0]
Use a dictionary when:
- labels matter
- you want named values
- you want to access data by keys like
person["name"]
Example of a list:
colors = ["red", "green", "blue"]
print(colors[0])
Example of a dictionary:
person = {"name": "Alice", "age": 25}
print(person["name"])
Lists use indexes like numbers. Dictionaries use keys like names or IDs.
If you want a broader comparison, see when to use lists vs tuples vs sets vs dictionaries.
Common beginner confusion
Here are some common mistakes beginners make with dictionaries.
- A dictionary is not the same as a list
- You do not get values with
person[0]unless0is actually a key - Missing keys can cause a
KeyError - Keys are often strings, but they do not have to be
Example of a mistake:
person = {"name": "Alice", "age": 25}
print(person[0])
This will cause an error because 0 is not a key in the dictionary.
Common mistakes
- Trying to access a dictionary value with a list-style index
- Using a key that does not exist
- Confusing keys with values
- Assuming dictionaries are only for strings
Example of a missing key:
person = {"name": "Alice"}
print(person["age"])
This raises a KeyError because "age" is not in the dictionary.
A safer option is often the get() method:
person = {"name": "Alice"}
print(person.get("age"))
Output:
None
To learn more, see Python dictionary get() and KeyError in Python: causes and fixes.
Useful debugging checks
If you are not sure what your dictionary contains, these simple checks help:
print(my_dict)
print(my_dict.keys())
print(type(my_dict))
print("name" in my_dict)
These can help you answer questions like:
- Is this really a dictionary?
- What keys does it have?
- Does a specific key exist?
Next steps
Once you understand the basic idea, the next step is using dictionaries in simple tasks.
Good next topics are:
- Python dictionaries explained
- How to access values in a dictionary in Python
- How to add a key to a dictionary in Python
- How to check if a key exists in a dictionary in Python
FAQ
What is a dictionary in Python in simple words?
It is a way to store data using names called keys and the data connected to them called values.
When should I use a dictionary instead of a list?
Use a dictionary when your data has labels, such as name, age, or price.
Can dictionary keys be numbers?
Yes. Keys can be numbers, strings, and some other immutable types.
Can a dictionary have duplicate keys?
No. Each key should be unique in the same dictionary.
Are Python dictionaries changeable?
Yes. Dictionaries are mutable, so you can add, remove, or update items.