Dictionaries in Python with Examples
#Dictionaries in Python with Examples
Introduction
In Python, a dictionary is one of the most powerful and widely used data structures. It allows developers to store data in key-value pairs, making it highly efficient for lookups, mappings, and structured data storage.
In this guide, you’ll learn:
What is a dictionary in Python?
How to create and use dictionaries.
Common dictionary operations with examples.
Best practices for using dictionaries effectively.
A dictionary in Python is an unordered, mutable collection where data is stored as key-value pairs. Each key in a dictionary must be unique and immutable (like strings, numbers, or tuples), while values can be of any data type.
Syntax:
dictionary_name = {key: value, key: value}
# Creating a dictionary
student = {
"name": "Alice",
"age": 22,
"course": "Computer Science"
}
print(student)
# Output: {'name': 'Alice', 'age': 22, 'course': 'Computer Science'}
✅ Keys are "name"
, "age"
, "course"
, and their values are "Alice"
, 22
, and "Computer Science"
.
# Accessing values using keys
print(student["name"]) # Output: Alice
print(student.get("age")) # Output: 22
# Adding a new key-value pair
student["grade"] = "A"
# Updating a value
student["age"] = 23
print(student)
# Output: {'name': 'Alice', 'age': 23, 'course': 'Computer Science', 'grade': 'A'}
# Removing a key-value pair
student.pop("course")
print(student) # Output: {'name': 'Alice', 'age': 23, 'grade': 'A'}
# Removing the last inserted item
student.popitem()
print(student) # Output: {'name': 'Alice', 'age': 23}
# Deleting a key
del student["age"]
print(student) # Output: {'name': 'Alice'}
person = {"name": "Bob", "age": 30, "city": "New York"}
# Loop through keys
for key in person:
print(key, ":", person[key])
# Loop through key-value pairs
for key, value in person.items():
print(key, "=>", value)
Output:
name : Bob
age : 30
city : New York
Method | Description | Example Output |
---|---|---|
dict.keys() | Returns all keys | ['name', 'age'] |
dict.values() | Returns all values | ['Alice', 22] |
dict.items() | Returns key-value pairs | [('name', 'Alice'), ('age', 22)] |
dict.update() | Updates dictionary with another dict | merged values |
dict.clear() | Removes all items | {} |
✅ Use dictionaries when data is structured as key-value mappings.
✅ Choose keys that are unique and descriptive.
✅ Use .get()
instead of direct access to avoid KeyError.
✅ Use dictionary comprehensions for concise transformations.
Example of Dictionary Comprehension:
squares = {x: x*x for x in range(1, 6)}
print(squares) # Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Dictionaries in Python provide a fast, flexible, and easy-to-use data structure for key-value mappings. Whether you’re handling JSON data, storing configuration details, or building applications, mastering dictionaries will make your Python coding more efficient.
"Learn Python dictionaries with examples. Understand key-value pairs, syntax, operations, methods, and best practices for using dictionaries in Python programming."
dictionaries in python, python dictionary example, python key-value pairs, python dictionary methods, dictionary comprehension python, python data structures, python dictionary tutorial