| id | title | description | sidebar_label | sidebar_position | tags | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
python-dictionaries |
Python Dictionaries |
Complete theoretical explanation of dictionaries in Python, covering creation, modification, methods, and use-cases. |
Python Dictionaries |
11 |
|
A dictionary in Python is an unordered, mutable, and indexed collection of key-value pairs. It is one of the most powerful and flexible built-in data structures in Python, suitable for representing structured data.
Dictionaries hold data in the form of key-value pairs. Each key is unique and maps to a specific value. Values can be of any data type, while keys must be immutable (like strings, numbers, or tuples).
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}- Keys are unique.
- Keys must be immutable.
- Values can be of any data type.
- Dictionaries are mutable and can be changed after creation.
- In Python 3.7+, dictionaries maintain insertion order.
data = {"a": 1, "b": 2}data = dict(x=10, y=20)empty = {}person["name"]person.get("age")
person.get("gender", "Not Found")person["gender"] = "Female"person["age"] = 30person.update({"age": 35, "city": "Chicago"})person.pop("age")del person["city"]person.clear()Removes and returns the last inserted key-value pair.
person.popitem()| Method | Description |
|---|---|
get(key) |
Returns value for key or None if key not found |
keys() |
Returns a view of all keys |
values() |
Returns a view of all values |
items() |
Returns a view of key-value pairs |
update() |
Updates dictionary with another dictionary |
pop(key) |
Removes specified key |
popitem() |
Removes the last inserted item |
clear() |
Removes all elements |
copy() |
Returns a shallow copy |
for key in person:
print(key)for value in person.values():
print(value)for key, value in person.items():
print(key, value)A dictionary can contain other dictionaries as values, enabling hierarchical data storage.
students = {
"101": {"name": "John", "grade": "A"},
"102": {"name": "Emma", "grade": "B"},
}
students["101"]["name"] # Output: JohnLike list comprehensions, dictionary comprehensions offer a concise way to create dictionaries.
squares = {x: x*x for x in range(1, 6)}- Representing JSON or structured data
- Frequency counting (e.g., word count)
- Lookup tables
- Configuration or settings
- Storing database records in memory
| Feature | Dictionary | List |
|---|---|---|
| Structure | Key-value pairs | Indexed elements |
| Access | Via key | Via index |
| Order | Insertion ordered (3.7+) | Ordered |
| Mutability | Mutable | Mutable |
| Use Case | Lookup, mapping | Sequence of items |
- Use
.get()instead of direct key access to avoidKeyError. - Use dictionary comprehension for cleaner and more readable code.
- Use keys that are hashable (e.g., strings, numbers).
- Use dictionaries for fast lookups and structured data representation.
- Dictionaries are one of the most versatile data structures in Python.
- They store key-value pairs and allow fast retrieval based on keys.
- Keys must be unique and immutable.
- Dictionaries support powerful methods for data manipulation and traversal.