Skip to content

4.2 Dictionaries – Understanding Dictionaries, Dictionary Operations

person holding sticky note

Wellcome to this insightful lesson where we’ll explore the powerful world of dictionaries in Python. Dictionaries are versatile data structures that allow us to store and retrieve data using key-value pairs. In this lesson, we’ll dive into the fundamentals of dictionaries, understand their structure, and explore various operations that can be performed on dictionaries.

Understanding Dictionaries:

1. Definition:

  • A dictionary is a collection of unordered, mutable, and indexed elements.
  • Elements are stored as key-value pairs, where each key must be unique.
  • Syntax:
my_dict = {"key1": value1, "key2": value2, ...}

2. Accessing Values:

  • Values can be accessed using their respective keys.
  • Example:
my_dict = {"name": "Alice", "age": 25, "city": "Wonderland"}
print("Name:", my_dict["name"])

3. Modifying Values:

  • Values associated with a key can be modified.
  • Example:
my_dict = {"name": "Alice", "age": 25, "city": "Wonderland"}
my_dict["age"] = 26

Dictionary Operations:

4. Adding Key-Value Pairs:

  • New key-value pairs can be added to a dictionary.
  • Example:
my_dict = {"name": "Alice", "age": 25}
my_dict["city"] = "Wonderland"

5. Removing Key-Value Pairs:

  • Key-value pairs can be removed using the del keyword or the pop() method.
  • Example:
my_dict = {"name": "Alice", "age": 25, "city": "Wonderland"}
del my_dict["age"]

6. Dictionary Methods:

  • Dictionaries have various built-in methods, such as keys(), values(), and items().
  • Example:
my_dict = {"name": "Alice", "age": 25, "city": "Wonderland"}
keys_list = my_dict.keys()
values_list = my_dict.values()

7. Dictionary Iteration:

  • Iterating through keys, values, or key-value pairs.
  • Example:
my_dict = {"name": "Alice", "age": 25, "city": "Wonderland"}
for key in my_dict:
    print(f"Key: {key}, Value: {my_dict[key]}")

Example:

Let’s consider an example where we use a dictionary to store information about a person:

# Example Code
person_info = {"name": "Bob", "age": 30, "occupation": "Engineer"}

# Accessing values
print("Name:", person_info["name"])
print("Age:", person_info["age"])

# Modifying a value
person_info["age"] = 31

# Adding a new key-value pair
person_info["location"] = "Cityville"

# Removing a key-value pair
del person_info["occupation"]

# Iterating through keys and values
for key, value in person_info.items():
    print(f"{key}: {value}")

Practice Exercise:

Create a Python script that uses a dictionary to represent a product in an online store. Include keys such as “product_name,” “price,” and “quantity_available.” Perform operations like modifying the price, adding a discount, and updating the quantity available.

# Example Practice Exercise
product_info = {"product_name": "Laptop", "price": 1200, "quantity_available": 50}

# Modifying the price
product_info["price"] = 1100

# Adding a discount
discount = 100
product_info["price_with_discount"] = product_info["price"] - discount

# Updating the quantity available
new_quantity = 40
product_info["quantity_available"] = new_quantity

# Displaying the updated information
for key, value in product_info.items():
    print(f"{key}: {value}")

Summary:

In this lesson, we’ve explored the fundamentals of dictionaries in Python and learned how to perform various operations on them. Dictionaries provide a flexible and efficient way to manage data using key-value pairs. Practice using dictionaries to enhance your coding skills, and feel free to ask questions in the discussion forum. Let’s continue our Python journey with confidence in utilizing dictionaries!