Real-world business data is structured. A customer has a name, an email, and a location. A product has a price, an SKU, and an inventory count. For this, we need dictionaries. A dictionary is the most important data structure for handling structured data in Python.
A dictionary is an unordered collection of key-value pairs.
"first_name"
)."John"
).customer = {
"customer_id": "CUST-001",
"first_name": "John",
"last_name": "Doe",
}
customer["first_name"]
). For safer access, use the .get()
method (customer.get("phone", "N/A")
), which returns a default value if the key doesn’t exist.customer["phone"] = "555-123-4567"
).del
keyword (del customer["company"]
).Before you begin, ensure you have followed the setup instructions in the main README.md to set up your virtual environment and install the required libraries.
The script for this lesson, dictionaries.py
, has been refactored to encapsulate dictionary operations into testable functions.
Day_08_Dictionaries/dictionaries.py
. Notice the functions like create_customer_profile()
, update_customer_record()
, and add_project_to_employee()
.Coding-For-MBA
), run the script to see the functions in action:
python Day_08_Dictionaries/dictionaries.py
pytest tests/test_day_08.py
Create a Product Dictionary:
my_solutions_08.py
), create a function create_product(product_id, name, price, in_stock, tags)
.PROD-123
, SuperWidget
, 199.99
, True
, ["electronics", "gadget"]
) and print the resulting dictionary.Modify Employee Information:
employee_record
dictionary from the lesson’s main block.add_project_to_employee
function.department
key of your new dictionary to “Marketing Director”.Access Nested Data:
company
with keys company_name
and headquarters
.headquarters
should be another dictionary with keys for city
, state
, and country
."[Company Name] is headquartered in [City], [State]."
, accessing the nested values to build the string.🎉 Amazing work! Dictionaries are the cornerstone of handling structured data in Python. Almost every time you get data from an API or a database, it will be in the form of dictionaries. Mastering them is a huge step forward.
Run this lesson’s code interactively in your browser:
!!! tip “About JupyterLite” JupyterLite runs entirely in your browser using WebAssembly. No installation or server required! Note: First launch may take a moment to load.
???+ example “dictionaries.py” View on GitHub
```python title="dictionaries.py"
"""
Day 8: Structuring Business Data with Dictionaries (Refactored)
This script demonstrates how to create, access, and modify
dictionaries that represent complex business records. This version
is refactored into functions for better organization and testability.
"""
def create_customer_profile(cust_id, first, last, email, company, is_premium, spent):
"""Creates a dictionary representing a customer profile."""
return {
"customer_id": cust_id,
"first_name": first,
"last_name": last,
"email": email,
"company": company,
"is_premium_member": is_premium,
"total_spent": spent,
}
def get_customer_attribute(customer_profile, attribute, default_value="N/A"):
"""Safely gets an attribute from a customer profile, returning a default if not found."""
return customer_profile.get(attribute, default_value)
def update_customer_record(customer_profile, key, value, is_new=False):
"""Updates or adds a key-value pair to a customer profile."""
profile_copy = customer_profile.copy()
if is_new:
profile_copy[key] = value
else:
# Assumes the key exists and we are modifying it (e.g., incrementing)
if key in profile_copy:
profile_copy[key] += value
return profile_copy
def remove_customer_attribute(customer_profile, attribute):
"""Removes an attribute from a customer profile if it exists."""
profile_copy = customer_profile.copy()
if attribute in profile_copy:
del profile_copy[attribute]
return profile_copy
def add_project_to_employee(employee_profile, new_project):
"""Adds a new project to an employee's project list."""
profile_copy = employee_profile.copy()
# Ensure the 'projects' key exists and is a list
if "projects" in profile_copy and isinstance(profile_copy["projects"], list):
# To avoid modifying a list within the original dict, we copy it too
profile_copy["projects"] = profile_copy["projects"].copy()
profile_copy["projects"].append(new_project)
return profile_copy
if __name__ == "__main__":
# --- Creating a Dictionary for a Customer Profile ---
print("--- Customer Profile Dictionary ---")
customer_record = create_customer_profile(
"CUST-001", "John", "Doe", "john.doe@example.com", "InnovateCorp", True, 2500.75
)
print(f"Original customer record: {customer_record}")
# Accessing data using keys
email_address = get_customer_attribute(customer_record, "email")
print(f"Customer's email: {email_address}")
# Safer access for a key that might not exist
phone_num = get_customer_attribute(customer_record, "phone")
print(f"Customer's phone: {phone_num}")
print()
# --- Modifying a Dictionary ---
print("--- Modifying Customer Record ---")
customer_record_with_phone = update_customer_record(
customer_record, "phone", "555-123-4567", is_new=True
)
print(
f"Added phone number: {get_customer_attribute(customer_record_with_phone, 'phone')}"
)
customer_record_updated_spent = update_customer_record(
customer_record_with_phone, "total_spent", 500.25
)
print(f"Updated total spent: ${customer_record_updated_spent['total_spent']:.2f}")
customer_record_no_premium = remove_customer_attribute(
customer_record_updated_spent, "is_premium_member"
)
print(f"Record after removing premium status: {customer_record_no_premium}")
print("-" * 20)
# --- Nested Dictionaries for Complex Structures ---
print("--- Nested Dictionary for an Employee Profile ---")
employee_record = {
"employee_id": "EMP-042",
"name": "Jane Smith",
"department": "Marketing",
"contact_info": {"email": "jane.smith@example.com", "phone_ext": 112},
"projects": ["Q1 Campaign", "Website Redesign"],
}
employee_email_address = employee_record.get("contact_info", {}).get("email")
print(f"Employee Email: {employee_email_address}")
first_proj = employee_record.get("projects", ["N/A"])[0]
print(f"First project assigned: {first_proj}")
employee_with_new_project = add_project_to_employee(
employee_record, "2025 Strategy"
)
print(f"Updated projects: {employee_with_new_project['projects']}")
print("-" * 20)
```
???+ example “solutions.py” View on GitHub
```python title="solutions.py"
"""
Day 8: Solutions to Exercises
"""
# --- Exercise 1: Create a Product Dictionary ---
print("--- Solution to Exercise 1 ---")
product = {
"product_id": "PROD-123",
"product_name": "SuperWidget",
"price": 199.99,
"in_stock": True,
"tags": ["electronics", "gadget"],
}
# Accessing the values using their keys
print(f"Product Name: {product['product_name']}")
print(f"Price: ${product['price']}")
print("-" * 20)
# --- Exercise 2: Modify Employee Information ---
print("--- Solution to Exercise 2 ---")
employee = {
"employee_id": "EMP-042",
"name": "Jane Smith",
"department": "Marketing",
"contact_info": {"email": "jane.smith@example.com", "phone_ext": 112},
"projects": ["Q1 Campaign", "Website Redesign"],
}
print(f"Original Employee Record: {employee}")
# Update the department
employee["department"] = "Marketing Director"
# Add a new project to the list of projects
employee["projects"].append("2025 Strategy")
print(f"Updated Employee Record: {employee}")
print("-" * 20)
# --- Exercise 3: Access Nested Data ---
print("--- Solution to Exercise 3 ---")
company = {
"company_name": "InnovateCorp",
"year_founded": 2015,
"headquarters": {"city": "New York", "state": "NY", "country": "USA"},
}
# Accessing the nested dictionary and then the values within it
company_name = company["company_name"]
hq_city = company["headquarters"]["city"]
hq_state = company["headquarters"]["state"]
print(f"{company_name} is headquartered in {hq_city}, {hq_state}.")
print("-" * 20)
```