Dictionaries

Introduction to Python for Developers

George Boorman

Curriculum Manager, DataCamp

Products list

prices = [10, 20, 30, 15, 25, 35]

# Product IDs product_ids = ["AG32", "HT91", "PL65", "OS31", "KB07", "TR48"]
Introduction to Python for Developers

Dictionary

  • Dictionary = key-value pairs

Dictionary annotated with the word (key) and definition (value)

1 https://unsplash.com/@sandym10
Introduction to Python for Developers

Why use dictionaries?

  • user_id
  • order_number
  • date
  • value
  • payment_method

 

  • ip_address
  • location

Cash

Computer network

1 https://unsplash.com/@sharonmccutcheon; https://unsplash.com/@jjying
Introduction to Python for Developers

Creating a dictionary

# Creating a dictionary
products_dict = 
Introduction to Python for Developers

Creating a dictionary

# Creating a dictionary
products_dict = {
Introduction to Python for Developers

Creating a dictionary

# Creating a dictionary
products_dict = {"AG32"
Introduction to Python for Developers

Creating a dictionary

# Creating a dictionary
products_dict = {"AG32":
Introduction to Python for Developers

Creating a dictionary

# Creating a dictionary
products_dict = {"AG32": 10
Introduction to Python for Developers

Creating a dictionary

# Creating a dictionary
products_dict = {"AG32": 10,
Introduction to Python for Developers

Creating a dictionary

# Creating a dictionary
products_dict = {"AG32": 10, "HT91": 20,
Introduction to Python for Developers

Creating a dictionary

# Creating a dictionary
products_dict = {"AG32": 10, "HT91": 20, 
                 "PL65": 30, "OS31": 15, 
                 "KB07": 25, "TR48": 35
Introduction to Python for Developers

Creating a dictionary

# Creating a dictionary
products_dict = {"AG32": 10, "HT91": 20, 
                 "PL65": 30, "OS31": 15, 
                 "KB07": 25, "TR48": 35}
Introduction to Python for Developers

Accessing a value based on the key

  • Dictionaries are ordered

    • Allows values to be accessed by subsetting on the key
  • What is the price of product ID "AG32"?

# Find the product's price
products_dict["AG32"]
10
Introduction to Python for Developers

Accessing all values

# Get all values from a dictionary
products_dict.values()
dict_values([10, 20, 30, 15, 25, 35])
Introduction to Python for Developers

Accessing all keys

# Retrieve all keys in a dictionary
products_dict.keys()
dict_keys(['AG32', 'HT91', 'PL65', 'OS31', 'KB07', 'TR48'])
Introduction to Python for Developers

Viewing an entire dictionary

# Print the dictionary
print(products_dict)
{'AG32': 10, 'HT91': 20, 'PL65': 30, 'OS31': 15, 'KB07': 25, 'TR48': 35}
# Get all items (key-value pairs)
products_dict.items()
dict_items([('AG32', 10), ('HT91', 20), ('PL65', 30), ('OS31', 15), ('KB07', 25),
('TR48', 35)])
  • .items() is useful when iterating or looping
Introduction to Python for Developers

Adding a key-value pair

# Add a new key-value pair
products_dict["UI56"] = 40
Introduction to Python for Developers

Updating a value

# Updating a value associated with an existing key
products_dict["HT91"] = 12
Introduction to Python for Developers

Duplicate keys

# Creating a dictionary with a duplicate key
products_dict = {"AG32": 10, "AG32": 20, 
                 "PL65": 30, "OS31": 15, 
                 "KB07": 25, "TR48": 35}

# Print the duplicate key's value print(products_dict["AG32"])
20
Introduction to Python for Developers

Let's practice!

Introduction to Python for Developers

Preparing Video For Download...