Python Dictionary: How to Create, Access, and Modify

A dictionary is a general-purpose data structure used to store key-value pairs. It is also known as an associative array, symbol table, or map. Dictionaries are helpful when working with structured data.
This article explains Python dictionaries with practical examples: how to create them, access values, update entries, and remove items.
Python Dictionaries
Dictionaries store data as key-value pairs. Each key must be unique and hashable, and each key maps to a value.
A dictionary is defined by enclosing a comma-separated list of key-value pairs in curly braces ({}):
dictionary_name = {key1: value1, key2: value2}- A comma (
,) separates key-value pairs. - A colon (
:) separates each key from its value.
Creating a Dictionary
You can create a dictionary using curly braces or the dict() constructor:
d = {"osname": "Ubuntu", "osversion": "24.04 LTS"}d = dict(osname="Ubuntu", osversion="24.04 LTS")You can also create a copy of another dictionary:
sysos = {"osname": "Ubuntu", "osversion": "24.04 LTS"}
newsysos = sysos.copy()Access Dictionary Values
Access values by key using square brackets:
nums = {"num1": 400, "num2": 300, "num3": 100}
nums["num3"]100If the key does not exist, Python raises a KeyError:
nums["num4"]Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'num4'Use in to check whether a key exists:
"num4" in nums
"num3" in numsFalse
TrueUse get() to read a key safely and return a default value if the key is missing:
nums.get("num4", 0)0Update a Dictionary
Update existing values or add new key-value pairs by assigning to a key:
nums = {"num1": 100, "num2": 200}
nums["num1"] = 300
nums["num3"] = 400
nums{'num1': 300, 'num2': 200, 'num3': 400}You can also update multiple values at once using update():
nums.update({"num2": 250, "num4": 500})Delete Items
To delete a key-value pair, use del or pop():
nums = {"num1": 100, "num2": 200}
del nums["num1"]
nums{'num2': 200}nums.pop("num2")200To delete all items, use clear():
nums.clear()
nums{}Dictionary Properties
Keys
Only immutable types are allowed as dictionary keys. A list, for example, is mutable, so it cannot be used as a key:
state = {[10, 20]: 90}Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'Common key types include:
- Integers
- Floats
- Strings
- Booleans
- Tuples
state = {"Texas": "Austin", 50: "California", ("New York", 80): 90, True: [10, 20, 30], 19.09: (1, 2, 3)}Keys must be unique. If duplicate keys are used, the last value wins:
employee_info = {"name": "Jack Smith", "age": 40, "experience": "20 years", "age": 92}
employee_info{'name': 'Jack Smith', 'age': 92, 'experience': '20 years'}Values
Values can be of any data type, including another dictionary or a list:
def addr():
return ["124C - Maryland", "USA", "N. America"]
student_info = {"name": "Jack Smith", "age": 24, "degree": True, "address": addr()}
student_info.values()dict_values(['Jack Smith', 24, True, ['124C - Maryland', 'USA', 'N. America']])Dictionaries Are Mutable and Ordered
Dictionaries are mutable, so you can update or delete items after creation. In Python 3.7 and later, dictionaries preserve insertion order.
Nested Dictionaries
You can nest dictionaries inside other dictionaries, which is useful for structured data:
students = {
"class A": {
"name": "Jack Smith",
"team": "Blue",
},
"class B": {
"name": "Lisa",
"team": "Green",
},
}
students["class B"]["name"]'Lisa'Dictionary Comprehension
Use a dictionary comprehension to build a dictionary from an iterable:
nums = [1, 2, 3]
squares = {n: n * n for n in nums}{1: 1, 2: 4, 3: 9}Iterating Over Dictionaries
You can loop through a dictionary using a for loop.
Loop through keys:
student = {"name": "Jack", "age": 24, "degree": "CS"}
for key in student:
print(key)name
age
degreeLoop through values:
for value in student.values():
print(value)Jack
24
CSLoop through both keys and values:
for key, value in student.items():
print(f"{key}: {value}")name: Jack
age: 24
degree: CSMerging Dictionaries
You can merge two dictionaries using update():
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
dict1.update(dict2)
dict1{'a': 1, 'b': 2, 'c': 3, 'd': 4}In Python 3.9 and later, use the | operator to merge dictionaries:
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
merged = dict1 | dict2{'a': 1, 'b': 2, 'c': 3, 'd': 4}Use |= to update a dictionary in place:
dict1 |= dict2Common Dictionary Methods
keys()- Return all keys.values()- Return all values.items()- Return key-value pairs.get(key, default)- Return value or default.update()- Update multiple items.pop(key)- Remove a key and return its value.popitem()- Remove and return the last inserted item.clear()- Remove all items.
student_info = {"name": "Jack Smith", "age": 24, "degree": True}
student_info.items()dict_items([('name', 'Jack Smith'), ('age', 24), ('degree', True)])Quick Reference
| Operation | Syntax |
|---|---|
| Create dictionary | d = {"key": "value"} |
| Access value | d["key"] or d.get("key") |
| Add/Update item | d["key"] = "value" |
| Delete item | del d["key"] or d.pop("key") |
| Check if key exists | "key" in d |
| Get all keys | d.keys() |
| Get all values | d.values() |
| Get all items | d.items() |
| Merge dictionaries | d1.update(d2) or d1 | d2 |
| Clear dictionary | d.clear() |
Conclusion
Python dictionaries store data as key-value pairs. You can create them, read values by key, update items, and remove entries as needed.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page