Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Dictionaries and Dictionary Methods | Other Data Types
Introduction to Python Video Course
course content

Conteúdo do Curso

Introduction to Python Video Course

Introduction to Python Video Course

1. Getting Started
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

Dictionaries and Dictionary Methods

Dictionaries are perhaps the most versatile Python data structure. They store data as key-value pairs and are essential for situations where data needs to be retrieved quickly, and modifications are frequent.

In our grocery store scenario, dictionaries could efficiently handle supplier information, allowing each supplier to be accessed by its name or ID without the need to search through a list.

Watch as Alex demonstrates how to utilize dictionaries for our grocery store:

Creation

Dictionaries are created by enclosing comma-separated key-value pairs in curly braces {}.

Ordering

Dictionaries preserve the insertion order of their elements, though it's essential to note that operations are typically conducted based on keys rather than position.

Mutability (Changeability)

Dictionaries are mutable, allowing you to add, update, or remove key-value pairs after the dictionary has been created;

Note

While dictionaries allow multiple values, each key must be unique within a dictionary. If a key is repeated during the assignment, the latest value will overwrite the previous, ensuring that each key has only one corresponding value.

Examples

Let's take a look at a simple dictionary. Instead of using index numbers, you access dictionary elements through their keys, which, in this case, are the names of the grocery items.

1234567891011
# Dictionary creation grocery_items = { "Milk": 3.49, "Eggs": 2.99, "Bread": 1.99, "Apples": 0.99 } # Extracting dictionary elements by their keys print("Price of Milk:", grocery_items["Milk"]) print("Price of Bread:", grocery_items["Bread"])
copy

Dictionaries in Python are flexible when it comes to the types of data they can store.

The only restriction is that keys must be of an immutable (unchangeable) type (such as strings, numbers, or tuples containing only immutable elements). This ensures that the key remains unchanged.

On the other hand, dictionary values can be of any type and may include mutable (changeable) types, such as lists or other dictionaries.

For instance:

123456789
# A dictionary with various types of keys and values store_info = { "Store Name": "Grocery Galore", # String key and string value 42: "Inventory Count", # Integer key and string value ("Bread", "Milk"): [2.99, 1.59] # Tuple key and list value (prices of bread and milk) } # Extracting dictionary element (list) by its key (tuple) print("Data under key ('Bread', 'Milk'):", store_info[("Bread", "Milk")])
copy

Dictionary Methods

Dictionaries provide a range of operations and methods that facilitate efficient data handling. Here are some of the most commonly used methods:

  • get(): retrieves the value for a specified key, and if the key is not found, it returns None. This is different from using square brackets (e.g., grocery_items["Milk"]), which would raise an error if the key doesn't exist.;
  • update(): updates the dictionary with elements from another dictionary or an iterable of key-value pairs, overwriting existing keys;
  • pop(): removes a specified key and returns the corresponding value.

Note

In Python, None is a special value that means "nothing" or "no value", and it's often used when you want to show that something is empty or doesn't have a result.

Example Application

Imagine you need to update the dictionary for an inventory in your grocery store. Here's how you can do it using dictionary methods:

12345678910111213141516171819202122
# Dictionary for a grocery store inventory inventory = { "Apples": 30, "Oranges": 18, "Bananas": 45 } # Get the count of Oranges print("Count of Oranges:", inventory.get("Oranges")) # Update inventory by adding a new item inventory.update({"Mangoes": 20}) print("Updated Inventory:", inventory) # You can also add a new item to the end of the dictionary like this inventory["Pineapples"] = 15 print("Updated Inventory:", inventory) # Remove Bananas from the inventory removed_item = inventory.pop("Bananas") print("Removed Item:", removed_item) print("Current Inventory:", inventory)
copy

Tarefa

You are responsible for maintaining an inventory system for a grocery store. The inventory is tracked using a dictionary where the item names are keys, and each value is a tuple containing the item's product ID and its category.

  1. Complete the definition of a dictionary grocery_inventory to store information about various grocery items. Each item should have a name as the key and a tuple containing its unique ID and category (such as "Dairy" or "Produce") as the value.
    ItemIDCategory
    Milk113Dairy
    Eggs116Dairy
    Bread117Bakery
    Apples141Produce
  2. Retrieve and print the details of the grocery item "Bread" from the dictionary by using an appropriate method to access the value for a given key.
  3. Add a new item, "Cookies", to the grocery_inventory dictionary. The item should have an ID of 143 and belong to the "Bakery" category.
  4. Remove the item "Eggs" from the grocery_inventory dictionary and print the updated inventory to confirm that the item has been removed.

Tarefa

You are responsible for maintaining an inventory system for a grocery store. The inventory is tracked using a dictionary where the item names are keys, and each value is a tuple containing the item's product ID and its category.

  1. Complete the definition of a dictionary grocery_inventory to store information about various grocery items. Each item should have a name as the key and a tuple containing its unique ID and category (such as "Dairy" or "Produce") as the value.
    ItemIDCategory
    Milk113Dairy
    Eggs116Dairy
    Bread117Bakery
    Apples141Produce
  2. Retrieve and print the details of the grocery item "Bread" from the dictionary by using an appropriate method to access the value for a given key.
  3. Add a new item, "Cookies", to the grocery_inventory dictionary. The item should have an ID of 143 and belong to the "Bakery" category.
  4. Remove the item "Eggs" from the grocery_inventory dictionary and print the updated inventory to confirm that the item has been removed.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 4. Capítulo 7
toggle bottom row

Dictionaries and Dictionary Methods

Dictionaries are perhaps the most versatile Python data structure. They store data as key-value pairs and are essential for situations where data needs to be retrieved quickly, and modifications are frequent.

In our grocery store scenario, dictionaries could efficiently handle supplier information, allowing each supplier to be accessed by its name or ID without the need to search through a list.

Watch as Alex demonstrates how to utilize dictionaries for our grocery store:

Creation

Dictionaries are created by enclosing comma-separated key-value pairs in curly braces {}.

Ordering

Dictionaries preserve the insertion order of their elements, though it's essential to note that operations are typically conducted based on keys rather than position.

Mutability (Changeability)

Dictionaries are mutable, allowing you to add, update, or remove key-value pairs after the dictionary has been created;

Note

While dictionaries allow multiple values, each key must be unique within a dictionary. If a key is repeated during the assignment, the latest value will overwrite the previous, ensuring that each key has only one corresponding value.

Examples

Let's take a look at a simple dictionary. Instead of using index numbers, you access dictionary elements through their keys, which, in this case, are the names of the grocery items.

1234567891011
# Dictionary creation grocery_items = { "Milk": 3.49, "Eggs": 2.99, "Bread": 1.99, "Apples": 0.99 } # Extracting dictionary elements by their keys print("Price of Milk:", grocery_items["Milk"]) print("Price of Bread:", grocery_items["Bread"])
copy

Dictionaries in Python are flexible when it comes to the types of data they can store.

The only restriction is that keys must be of an immutable (unchangeable) type (such as strings, numbers, or tuples containing only immutable elements). This ensures that the key remains unchanged.

On the other hand, dictionary values can be of any type and may include mutable (changeable) types, such as lists or other dictionaries.

For instance:

123456789
# A dictionary with various types of keys and values store_info = { "Store Name": "Grocery Galore", # String key and string value 42: "Inventory Count", # Integer key and string value ("Bread", "Milk"): [2.99, 1.59] # Tuple key and list value (prices of bread and milk) } # Extracting dictionary element (list) by its key (tuple) print("Data under key ('Bread', 'Milk'):", store_info[("Bread", "Milk")])
copy

Dictionary Methods

Dictionaries provide a range of operations and methods that facilitate efficient data handling. Here are some of the most commonly used methods:

  • get(): retrieves the value for a specified key, and if the key is not found, it returns None. This is different from using square brackets (e.g., grocery_items["Milk"]), which would raise an error if the key doesn't exist.;
  • update(): updates the dictionary with elements from another dictionary or an iterable of key-value pairs, overwriting existing keys;
  • pop(): removes a specified key and returns the corresponding value.

Note

In Python, None is a special value that means "nothing" or "no value", and it's often used when you want to show that something is empty or doesn't have a result.

Example Application

Imagine you need to update the dictionary for an inventory in your grocery store. Here's how you can do it using dictionary methods:

12345678910111213141516171819202122
# Dictionary for a grocery store inventory inventory = { "Apples": 30, "Oranges": 18, "Bananas": 45 } # Get the count of Oranges print("Count of Oranges:", inventory.get("Oranges")) # Update inventory by adding a new item inventory.update({"Mangoes": 20}) print("Updated Inventory:", inventory) # You can also add a new item to the end of the dictionary like this inventory["Pineapples"] = 15 print("Updated Inventory:", inventory) # Remove Bananas from the inventory removed_item = inventory.pop("Bananas") print("Removed Item:", removed_item) print("Current Inventory:", inventory)
copy

Tarefa

You are responsible for maintaining an inventory system for a grocery store. The inventory is tracked using a dictionary where the item names are keys, and each value is a tuple containing the item's product ID and its category.

  1. Complete the definition of a dictionary grocery_inventory to store information about various grocery items. Each item should have a name as the key and a tuple containing its unique ID and category (such as "Dairy" or "Produce") as the value.
    ItemIDCategory
    Milk113Dairy
    Eggs116Dairy
    Bread117Bakery
    Apples141Produce
  2. Retrieve and print the details of the grocery item "Bread" from the dictionary by using an appropriate method to access the value for a given key.
  3. Add a new item, "Cookies", to the grocery_inventory dictionary. The item should have an ID of 143 and belong to the "Bakery" category.
  4. Remove the item "Eggs" from the grocery_inventory dictionary and print the updated inventory to confirm that the item has been removed.

Tarefa

You are responsible for maintaining an inventory system for a grocery store. The inventory is tracked using a dictionary where the item names are keys, and each value is a tuple containing the item's product ID and its category.

  1. Complete the definition of a dictionary grocery_inventory to store information about various grocery items. Each item should have a name as the key and a tuple containing its unique ID and category (such as "Dairy" or "Produce") as the value.
    ItemIDCategory
    Milk113Dairy
    Eggs116Dairy
    Bread117Bakery
    Apples141Produce
  2. Retrieve and print the details of the grocery item "Bread" from the dictionary by using an appropriate method to access the value for a given key.
  3. Add a new item, "Cookies", to the grocery_inventory dictionary. The item should have an ID of 143 and belong to the "Bakery" category.
  4. Remove the item "Eggs" from the grocery_inventory dictionary and print the updated inventory to confirm that the item has been removed.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 4. Capítulo 7
toggle bottom row

Dictionaries and Dictionary Methods

Dictionaries are perhaps the most versatile Python data structure. They store data as key-value pairs and are essential for situations where data needs to be retrieved quickly, and modifications are frequent.

In our grocery store scenario, dictionaries could efficiently handle supplier information, allowing each supplier to be accessed by its name or ID without the need to search through a list.

Watch as Alex demonstrates how to utilize dictionaries for our grocery store:

Creation

Dictionaries are created by enclosing comma-separated key-value pairs in curly braces {}.

Ordering

Dictionaries preserve the insertion order of their elements, though it's essential to note that operations are typically conducted based on keys rather than position.

Mutability (Changeability)

Dictionaries are mutable, allowing you to add, update, or remove key-value pairs after the dictionary has been created;

Note

While dictionaries allow multiple values, each key must be unique within a dictionary. If a key is repeated during the assignment, the latest value will overwrite the previous, ensuring that each key has only one corresponding value.

Examples

Let's take a look at a simple dictionary. Instead of using index numbers, you access dictionary elements through their keys, which, in this case, are the names of the grocery items.

1234567891011
# Dictionary creation grocery_items = { "Milk": 3.49, "Eggs": 2.99, "Bread": 1.99, "Apples": 0.99 } # Extracting dictionary elements by their keys print("Price of Milk:", grocery_items["Milk"]) print("Price of Bread:", grocery_items["Bread"])
copy

Dictionaries in Python are flexible when it comes to the types of data they can store.

The only restriction is that keys must be of an immutable (unchangeable) type (such as strings, numbers, or tuples containing only immutable elements). This ensures that the key remains unchanged.

On the other hand, dictionary values can be of any type and may include mutable (changeable) types, such as lists or other dictionaries.

For instance:

123456789
# A dictionary with various types of keys and values store_info = { "Store Name": "Grocery Galore", # String key and string value 42: "Inventory Count", # Integer key and string value ("Bread", "Milk"): [2.99, 1.59] # Tuple key and list value (prices of bread and milk) } # Extracting dictionary element (list) by its key (tuple) print("Data under key ('Bread', 'Milk'):", store_info[("Bread", "Milk")])
copy

Dictionary Methods

Dictionaries provide a range of operations and methods that facilitate efficient data handling. Here are some of the most commonly used methods:

  • get(): retrieves the value for a specified key, and if the key is not found, it returns None. This is different from using square brackets (e.g., grocery_items["Milk"]), which would raise an error if the key doesn't exist.;
  • update(): updates the dictionary with elements from another dictionary or an iterable of key-value pairs, overwriting existing keys;
  • pop(): removes a specified key and returns the corresponding value.

Note

In Python, None is a special value that means "nothing" or "no value", and it's often used when you want to show that something is empty or doesn't have a result.

Example Application

Imagine you need to update the dictionary for an inventory in your grocery store. Here's how you can do it using dictionary methods:

12345678910111213141516171819202122
# Dictionary for a grocery store inventory inventory = { "Apples": 30, "Oranges": 18, "Bananas": 45 } # Get the count of Oranges print("Count of Oranges:", inventory.get("Oranges")) # Update inventory by adding a new item inventory.update({"Mangoes": 20}) print("Updated Inventory:", inventory) # You can also add a new item to the end of the dictionary like this inventory["Pineapples"] = 15 print("Updated Inventory:", inventory) # Remove Bananas from the inventory removed_item = inventory.pop("Bananas") print("Removed Item:", removed_item) print("Current Inventory:", inventory)
copy

Tarefa

You are responsible for maintaining an inventory system for a grocery store. The inventory is tracked using a dictionary where the item names are keys, and each value is a tuple containing the item's product ID and its category.

  1. Complete the definition of a dictionary grocery_inventory to store information about various grocery items. Each item should have a name as the key and a tuple containing its unique ID and category (such as "Dairy" or "Produce") as the value.
    ItemIDCategory
    Milk113Dairy
    Eggs116Dairy
    Bread117Bakery
    Apples141Produce
  2. Retrieve and print the details of the grocery item "Bread" from the dictionary by using an appropriate method to access the value for a given key.
  3. Add a new item, "Cookies", to the grocery_inventory dictionary. The item should have an ID of 143 and belong to the "Bakery" category.
  4. Remove the item "Eggs" from the grocery_inventory dictionary and print the updated inventory to confirm that the item has been removed.

Tarefa

You are responsible for maintaining an inventory system for a grocery store. The inventory is tracked using a dictionary where the item names are keys, and each value is a tuple containing the item's product ID and its category.

  1. Complete the definition of a dictionary grocery_inventory to store information about various grocery items. Each item should have a name as the key and a tuple containing its unique ID and category (such as "Dairy" or "Produce") as the value.
    ItemIDCategory
    Milk113Dairy
    Eggs116Dairy
    Bread117Bakery
    Apples141Produce
  2. Retrieve and print the details of the grocery item "Bread" from the dictionary by using an appropriate method to access the value for a given key.
  3. Add a new item, "Cookies", to the grocery_inventory dictionary. The item should have an ID of 143 and belong to the "Bakery" category.
  4. Remove the item "Eggs" from the grocery_inventory dictionary and print the updated inventory to confirm that the item has been removed.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Dictionaries are perhaps the most versatile Python data structure. They store data as key-value pairs and are essential for situations where data needs to be retrieved quickly, and modifications are frequent.

In our grocery store scenario, dictionaries could efficiently handle supplier information, allowing each supplier to be accessed by its name or ID without the need to search through a list.

Watch as Alex demonstrates how to utilize dictionaries for our grocery store:

Creation

Dictionaries are created by enclosing comma-separated key-value pairs in curly braces {}.

Ordering

Dictionaries preserve the insertion order of their elements, though it's essential to note that operations are typically conducted based on keys rather than position.

Mutability (Changeability)

Dictionaries are mutable, allowing you to add, update, or remove key-value pairs after the dictionary has been created;

Note

While dictionaries allow multiple values, each key must be unique within a dictionary. If a key is repeated during the assignment, the latest value will overwrite the previous, ensuring that each key has only one corresponding value.

Examples

Let's take a look at a simple dictionary. Instead of using index numbers, you access dictionary elements through their keys, which, in this case, are the names of the grocery items.

1234567891011
# Dictionary creation grocery_items = { "Milk": 3.49, "Eggs": 2.99, "Bread": 1.99, "Apples": 0.99 } # Extracting dictionary elements by their keys print("Price of Milk:", grocery_items["Milk"]) print("Price of Bread:", grocery_items["Bread"])
copy

Dictionaries in Python are flexible when it comes to the types of data they can store.

The only restriction is that keys must be of an immutable (unchangeable) type (such as strings, numbers, or tuples containing only immutable elements). This ensures that the key remains unchanged.

On the other hand, dictionary values can be of any type and may include mutable (changeable) types, such as lists or other dictionaries.

For instance:

123456789
# A dictionary with various types of keys and values store_info = { "Store Name": "Grocery Galore", # String key and string value 42: "Inventory Count", # Integer key and string value ("Bread", "Milk"): [2.99, 1.59] # Tuple key and list value (prices of bread and milk) } # Extracting dictionary element (list) by its key (tuple) print("Data under key ('Bread', 'Milk'):", store_info[("Bread", "Milk")])
copy

Dictionary Methods

Dictionaries provide a range of operations and methods that facilitate efficient data handling. Here are some of the most commonly used methods:

  • get(): retrieves the value for a specified key, and if the key is not found, it returns None. This is different from using square brackets (e.g., grocery_items["Milk"]), which would raise an error if the key doesn't exist.;
  • update(): updates the dictionary with elements from another dictionary or an iterable of key-value pairs, overwriting existing keys;
  • pop(): removes a specified key and returns the corresponding value.

Note

In Python, None is a special value that means "nothing" or "no value", and it's often used when you want to show that something is empty or doesn't have a result.

Example Application

Imagine you need to update the dictionary for an inventory in your grocery store. Here's how you can do it using dictionary methods:

12345678910111213141516171819202122
# Dictionary for a grocery store inventory inventory = { "Apples": 30, "Oranges": 18, "Bananas": 45 } # Get the count of Oranges print("Count of Oranges:", inventory.get("Oranges")) # Update inventory by adding a new item inventory.update({"Mangoes": 20}) print("Updated Inventory:", inventory) # You can also add a new item to the end of the dictionary like this inventory["Pineapples"] = 15 print("Updated Inventory:", inventory) # Remove Bananas from the inventory removed_item = inventory.pop("Bananas") print("Removed Item:", removed_item) print("Current Inventory:", inventory)
copy

Tarefa

You are responsible for maintaining an inventory system for a grocery store. The inventory is tracked using a dictionary where the item names are keys, and each value is a tuple containing the item's product ID and its category.

  1. Complete the definition of a dictionary grocery_inventory to store information about various grocery items. Each item should have a name as the key and a tuple containing its unique ID and category (such as "Dairy" or "Produce") as the value.
    ItemIDCategory
    Milk113Dairy
    Eggs116Dairy
    Bread117Bakery
    Apples141Produce
  2. Retrieve and print the details of the grocery item "Bread" from the dictionary by using an appropriate method to access the value for a given key.
  3. Add a new item, "Cookies", to the grocery_inventory dictionary. The item should have an ID of 143 and belong to the "Bakery" category.
  4. Remove the item "Eggs" from the grocery_inventory dictionary and print the updated inventory to confirm that the item has been removed.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 4. Capítulo 7
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt