How to create and work with Python Dictionaries

Introduction

Welcome ! In this article, we will learn what dictionaries are, how to use them, and see some real-world business examples. Dictionaries are a powerful data structure that allows you to store and manage data using key-value pairs. This makes them incredibly useful for a wide range of applications.

What are Dictionaries?

A dictionary is a collection of items where each item consists of a key and a value. Dictionaries are unordered, changeable, and indexed by keys, which must be unique. They are written with curly braces {}, with keys and values separated by a colon :.

Example:

Let’s create a simple dictionary to store the ages of different people:

ages = {"Alice": 25, "Bob": 30, "Charlie": 35}
print(ages)

This code will output:

{'Alice': 25, 'Bob': 30, 'Charlie': 35}
Accessing Values

You can access the value associated with a specific key by using the key inside square brackets.

print(ages["Alice"])  # Output: 25
print(ages["Bob"]) # Output: 30
Adding and Modifying Items

You can add a new key-value pair or modify an existing one by using the assignment operator =.

ages["Diana"] = 40  # Adding a new key-value pair
print(ages) # Output: {'Alice': 25, 'Bob': 30, 'Charlie': 35, 'Diana': 40}

ages["Alice"] = 26 # Modifying an existing key-value pair
print(ages) # Output: {'Alice': 26, 'Bob': 30, 'Charlie': 35, 'Diana': 40}
Removing Items

You can remove a key-value pair using the del statement or the pop() method.

del ages["Charlie"]  # Removing a key-value pair
print(ages) # Output: {'Alice': 26, 'Bob': 30, 'Diana': 40}

ages.pop("Bob") # Removing a key-value pair using pop()
print(ages) # Output: {'Alice': 26, 'Diana': 40}

Real-Time Business Examples

Example 1: Employee Records

Let’s say we want to store information about employees in a company, including their ID, name, and department.

employee_records = {
"E001": {"name": "John Doe", "department": "Sales"},
"E002": {"name": "Jane Smith", "department": "Marketing"},
"E003": {"name": "Sam Brown", "department": "IT"}
}

# Accessing an employee's details
print(employee_records["E001"])
# Output: {'name': 'John Doe', 'department': 'Sales'}

# Adding a new employee
employee_records["E004"] = {"name": "Sara White", "department": "HR"}
print(employee_records)
# Output: {'E001': {'name': 'John Doe', 'department': 'Sales'}, 'E002': {'name': 'Jane Smith', 'department': 'Marketing'}, 'E003': {'name': 'Sam Brown', 'department': 'IT'}, 'E004': {'name': 'Sara White', 'department': 'HR'}}
Example 2: Product Inventory

In a store, we can use a dictionary to keep track of product inventory, including product name, price, and quantity in stock.

inventory = {
"P001": {"name": "Laptop", "price": 800, "quantity": 10},
"P002": {"name": "Smartphone", "price": 500, "quantity": 15},
"P003": {"name": "Tablet", "price": 300, "quantity": 20}
}

# Accessing a product's details
print(inventory["P001"])
# Output: {'name': 'Laptop', 'price': 800, 'quantity': 10}

# Updating the quantity of a product
inventory["P002"]["quantity"] -= 1
print(inventory["P002"])
# Output: {'name': 'Smartphone', 'price': 500, 'quantity': 14}

Conclusion

In this article, we explored dictionaries in Python, a versatile data structure that uses key-value pairs. We learned how to create, access, modify, and remove items in a dictionary. We also saw how dictionaries can be used in real-world business scenarios like managing employee records and product inventory.

Practice Exercise

  1. Create a dictionary to store details of three of your friends, including their name, age, and city.
  2. Access and print the details of one friend using their key.
  3. Add a new friend to the dictionary and print the updated dictionary.
  4. Remove a friend from the dictionary and print the updated dictionary.

Happy coding!

Leave a Reply