Dictionary in Python

In a NoSQL database, data structure varies based on requirements. In a List, data is stored in a specific order, and the position of each element matters. For example, to access the first element in a List, you retrieve the value at index 0, which is predictable due to the ordered nature of Lists. However, when the order of data is not important, a Dictionary (or key-value store) is more suitable. Consider a scenario where you need to store user information like age, address, first name, and last name. In this case, the sequence of these fields doesn’t matter. With a Dictionary, you can directly access a specific value using its key, such as retrieving the first name by querying the "first_name" key, without needing to know the last name or other fields. Key differences: In a List, values are stored with predefined, sequential indices (0, 1, 2, ...), and you access data by its position. In a Dictionary, you define custom keys mapped to values, allowing direct access to data via the key, regardless of order. Thus, for scenarios where you need flexible, key-based access to data without relying on order, a Dictionary is the preferred choice. Coffee Menu in Python — Dictionary Operations Creating a Coffee Menu (Dictionary) coffee_menu = { "Espresso": "Bold shot!", "Latte": "Creamy delight", "Cappuccino": "Frothy classic", "Mocha": "Choco-coffee", "ColdBrew": "Smooth chill", "Americano": "Espresso + water", "Macchiato": "Espresso kiss" } print(coffee_menu) Accessing a Value (Using Key) print(coffee_menu['Espresso']) Output: Bold shot! Changing a Value coffee_menu['Espresso'] = 'Tiny Thunder' print(coffee_menu) Output: { 'Espresso': 'Tiny Thunder', 'Latte': 'Creamy delight', 'Cappuccino': 'Frothy classic', 'Mocha': 'Choco-coffee', 'ColdBrew': 'Smooth chill', 'Americano': 'Espresso + water', 'Macchiato': 'Espresso kiss' } Iterating Over the Dictionary Only Keys for coffee in coffee_menu: print(coffee) Output: Espresso Latte Cappuccino Mocha ColdBrew Americano Macchiato Keys and Values (Method 1) for coffee in coffee_menu: print(coffee, ":", coffee_menu[coffee]) Keys and Values (Method 2) for key, value in coffee_menu.items(): print(key, ":", value) Adding an Item coffee_menu["Flat White"] = "Velvety Smooth" print(coffee_menu) Removing Items Using pop(key) coffee_menu.pop('Espresso') print(coffee_menu) Output: { 'Latte': 'Creamy delight', 'Cappuccino': 'Frothy classic', 'Mocha': 'Choco-coffee', 'ColdBrew': 'Smooth chill', 'Americano': 'Espresso + water', 'Macchiato': 'Espresso kiss' } Using popitem() Removes the last inserted item. coffee_menu.popitem() print(coffee_menu) Example Output: { 'Espresso': 'Bold shot!', 'Latte': 'Creamy delight', 'Cappuccino': 'Frothy classic', 'Mocha': 'Choco-coffee', 'ColdBrew': 'Smooth chill', 'Americano': 'Espresso + water' } ⚠️ Note: If the dictionary is empty, popitem() will raise a KeyError. Using del Statement del coffee_menu["Latte"] print(coffee_menu)

Jun 10, 2025 - 20:30
 0
Dictionary in Python

In a NoSQL database, data structure varies based on requirements. In a List, data is stored in a specific order, and the position of each element matters. For example, to access the first element in a List, you retrieve the value at index 0, which is predictable due to the ordered nature of Lists.

However, when the order of data is not important, a Dictionary (or key-value store) is more suitable. Consider a scenario where you need to store user information like age, address, first name, and last name. In this case, the sequence of these fields doesn’t matter. With a Dictionary, you can directly access a specific value using its key, such as retrieving the first name by querying the "first_name" key, without needing to know the last name or other fields.

Key differences:

  • In a List, values are stored with predefined, sequential indices (0, 1, 2, ...), and you access data by its position.
  • In a Dictionary, you define custom keys mapped to values, allowing direct access to data via the key, regardless of order.

Thus, for scenarios where you need flexible, key-based access to data without relying on order, a Dictionary is the preferred choice.

Coffee Menu in Python — Dictionary Operations

  1. Creating a Coffee Menu (Dictionary)
coffee_menu = {
    "Espresso": "Bold shot!",
    "Latte": "Creamy delight",
    "Cappuccino": "Frothy classic",
    "Mocha": "Choco-coffee",
    "ColdBrew": "Smooth chill",
    "Americano": "Espresso + water",
    "Macchiato": "Espresso kiss"
}
print(coffee_menu)
  1. Accessing a Value (Using Key)
print(coffee_menu['Espresso'])

Output:

Bold shot!
  1. Changing a Value
    coffee_menu['Espresso'] = 'Tiny Thunder'
    print(coffee_menu)
    

Output:

{
    'Espresso': 'Tiny Thunder',
    'Latte': 'Creamy delight',
    'Cappuccino': 'Frothy classic',
    'Mocha': 'Choco-coffee',
    'ColdBrew': 'Smooth chill',
    'Americano': 'Espresso + water',
    'Macchiato': 'Espresso kiss'
}
  1. Iterating Over the Dictionary
  • Only Keys
for coffee in coffee_menu:
    print(coffee)

Output:

Espresso
Latte
Cappuccino
Mocha
ColdBrew
Americano
Macchiato
  • Keys and Values (Method 1)

    for coffee in coffee_menu:
    print(coffee, ":", coffee_menu[coffee])
    
  • Keys and Values (Method 2)

    for key, value in coffee_menu.items():
    print(key, ":", value)
    
  1. Adding an Item

    coffee_menu["Flat White"] = "Velvety Smooth"
    print(coffee_menu)
    
  2. Removing Items

  • Using pop(key)
    coffee_menu.pop('Espresso')
    print(coffee_menu)
    

Output:

{
    'Latte': 'Creamy delight',
    'Cappuccino': 'Frothy classic',
    'Mocha': 'Choco-coffee',
    'ColdBrew': 'Smooth chill',
    'Americano': 'Espresso + water',
    'Macchiato': 'Espresso kiss'
}
  • Using popitem()

Removes the last inserted item.

coffee_menu.popitem()
print(coffee_menu)

Example Output:

{
    'Espresso': 'Bold shot!',
    'Latte': 'Creamy delight',
    'Cappuccino': 'Frothy classic',
    'Mocha': 'Choco-coffee',
    'ColdBrew': 'Smooth chill',
    'Americano': 'Espresso + water'
}

⚠️ Note: If the dictionary is empty, popitem() will raise a KeyError.

  • Using del Statement
    del coffee_menu["Latte"]
    print(coffee_menu)