Dictionaries in Python#
What is a Dictionary?#
A dictionary in Python is an unordered collection of data in a key-value pair format. Dictionaries are mutable and indexed by keys, which can be any immutable type.
Syntax:
my_dict = {
"name": "Alice",
"age": 30,
"city": "New York"
}
Creating a Dictionary#
You can create a dictionary in several ways:
# Using curly braces
student = {"name": "John", "grade": "A"}
# Using the dict() constructor
student = dict(name="John", grade="A")
Accessing Elements#
Use the key inside square brackets or the get() method.
name = student["name"]
grade = student.get("grade")
Modifying Elements#
You can update a value using the assignment operator.
student["grade"] = "A+"
Adding Elements#
To add a new key-value pair:
student["age"] = 20
Removing Elements#
Several methods can be used:
del student["age"]
student.pop("grade")
student.clear() # Removes all items
Dictionary Methods#
Common methods used with dictionaries:
keys = student.keys()
values = student.values()
items = student.items()
Iterating Through a Dictionary#
for key, value in student.items():
print(key, value)
Nested Dictionaries#
people = {
"person1": {"name": "Alice", "age": 25},
"person2": {"name": "Bob", "age": 30}
}
Dictionary Comprehension#
squares = {x: x*x for x in range(5)}
Real-Life Usage Examples#
Machine Learning: Feature Representation
Dictionaries are used for encoding features before passing to models.
sample = {
"age": 29,
"gender": "female",
"income": 45000
}
features = {
"age": sample["age"],
"gender_female": 1 if sample["gender"] == "female" else 0,
"income": sample["income"]
}
Chatbot Intent Mapping
Mapping user intents to bot responses using dictionaries.
intents = {
"greeting": "Hello! How can I help you today?",
"farewell": "Goodbye! Have a great day.",
"thanks": "You're welcome!"
}
user_input = "greeting"
print(intents.get(user_input, "Sorry, I didn’t understand that."))
Configuration Settings
Storing application settings in dictionaries for easy access and modification.
config = {
"debug": True,
"database_uri": "sqlite:///mydb.db",
"max_connections": 20
}
if config["debug"]:
print("Debug mode is ON")