Dictionaries

Introduzione a Python per sviluppatori

Jasmin Ludolf

Senior Data Science Content Developer

Ingredients list

ingredients = ["pasta", "tomatoes", "garlic", "basil", "olive oil", "salt"]

# Ingredient quantities (in grams) quantities = [500, 400, 15, 20, 30, 5]
Introduzione a Python per sviluppatori

Dictionary

  • Dictionary = key-value pairs

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

1 https://unsplash.com/@sandym10
Introduzione a Python per sviluppatori

Why use dictionaries?

  • username
  • email
  • preferences

 

  • ip_address
  • location

$$

  • Recipe scaler: link ingredients to quantities

User info

Computer network

1 User image by Bilobaba Vladimir, Aviavlad; https://unsplash.com/@jjying
Introduzione a Python per sviluppatori

Creating a dictionary

# Creating a dictionary
recipe = 
Introduzione a Python per sviluppatori

Creating a dictionary

# Creating a dictionary
recipe = {
Introduzione a Python per sviluppatori

Creating a dictionary

# Creating a dictionary
recipe = {"pasta"
Introduzione a Python per sviluppatori

Creating a dictionary

# Creating a dictionary
recipe = {"pasta":
Introduzione a Python per sviluppatori

Creating a dictionary

# Creating a dictionary
recipe = {"pasta": 500
Introduzione a Python per sviluppatori

Creating a dictionary

# Creating a dictionary
recipe = {"pasta": 500,
Introduzione a Python per sviluppatori

Creating a dictionary

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400,
Introduzione a Python per sviluppatori

Creating a dictionary

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5
Introduzione a Python per sviluppatori

Creating a dictionary

# Creating a dictionary
recipe = {"pasta": 500,
          "tomatoes": 400, 
          "garlic": 15,
          "basil": 20, 
          "olive oil": 30,
          "salt": 5}
Introduzione a Python per sviluppatori

Accessing a value based on the key

  • Dictionaries are ordered

    • Allows values to be accessed by subsetting on the key
  • How much pasta do we need?

# Find the ingredient's quantity
print(recipe["pasta"])
500
Introduzione a Python per sviluppatori

Accessing all values

# Get all values from a dictionary
print(recipe.values())
dict_values([500, 400, 15, 20, 30, 5])
Introduzione a Python per sviluppatori

Accessing all keys

# Retrieve all keys in a dictionary
print(recipe.keys())
dict_keys(['pasta', 'tomatoes', 'garlic', 'basil', 'olive oil', 'salt'])
Introduzione a Python per sviluppatori

Viewing an entire dictionary

# Print the dictionary
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5}
# Get all items (key-value pairs)
print(recipe.items())
dict_items([('pasta', 500), ('tomatoes', 400), ('garlic', 15), ('basil', 20), 
('olive oil', 30), ('salt', 5)])
Introduzione a Python per sviluppatori

Adding a key-value pair

# Add a new key-value pair
recipe["parmesan"] = 50
print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
Introduzione a Python per sviluppatori

Updating a value

print(recipe)
{'pasta': 500, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
# Updating a value associated with an existing key
recipe["pasta"] = 1000
print(recipe)
{'pasta': 1000, 'tomatoes': 400, 'garlic': 15, 'basil': 20, 'olive oil': 30,
'salt': 5, 'parmesan': 50}
Introduzione a Python per sviluppatori

Duplicate keys

# Creating a dictionary with a duplicate key
recipe = {"pasta": 500, "garlic": 5, 
          "garlic": 15, "basil": 20, 
          "olive oil": 30, "salt": 5}

# Print the duplicate key's value print(recipe["garlic"])
15
Introduzione a Python per sviluppatori

Let's practice!

Introduzione a Python per sviluppatori

Preparing Video For Download...