Learning objectives
By the end of this section you should be able to
- Explain the structure of nested dictionaries.
- Use dictionary comprehension to create a dictionary object.
Nested dictionaries
As described before, Python dictionaries are a type of data structure that allows for storing data in key-value pairs. Nested dictionaries are dictionaries that are stored as values within another dictionary. Ex: An organizational chart with keys being different departments and values being dictionaries of employees in a given department. For storing employee information in a department, a dictionary can be used with keys being employee IDs and values being employee names. The tables below outline the structure of such nested dictionaries and how nested values can be accessed.
Example 10.4
Defining nested dictionaries and accessing elements
Defining nested dictionaries |
---|
company_org_chart = {
"Marketing": {
"ID234": "Jane Smith"
},
"Sales": {
"ID123": "Bob Johnson",
"ID122": "David Lee"
},
"Engineering": {
"ID303": "Radhika Potlapally",
"ID321": "Maryam Samimi"
}
}
|
Accessing nested dictionary items |
---|
print(company_org_chart["Sales"]["ID122"])
print(company_org_chart["Engineering"]["ID321"])
David Lee Maryam Samimi |
Concepts in Practice
Nested dictionary structure
Dictionary comprehension
Dictionary comprehension is a concise and efficient way to create a dictionary in Python. With dictionary comprehension, elements of an iterable object are transformed into key-value pairs. The syntax of dictionary comprehension is similar to list comprehension, but instead of using square brackets, curly braces are used to define a dictionary.
Here is a general syntax for dictionary comprehension:
Syntax for dictionary comprehension
{key_expression: value_expression for element in iterable}
Concepts in Practice
Dictionary comprehension
Try It
Product prices
Suppose you have a dictionary of product prices, where the keys are product names and the values are their respective prices in dollars. Write a Python program that uses dictionary comprehension to create a new dictionary that has the same keys as the original dictionary, but the values are the prices in euros. Assume that the exchange rate is 1 dollar = 0.85 euros.
Try It
Restructuring the company data
Suppose you have a dictionary that contains information about employees at a company. Each employee is identified by an ID number, and their information includes their name, department, and salary. You want to create a nested dictionary that groups employees by department so that you can easily see the names and salaries of all employees in each department. Write a Python program that when given a dictionary, employees
, outputs a nested dictionary, dept_employees
, which groups employees by department.
Input:
employees = {
1001: {"name": "Alice", "department": "Engineering", "salary": 75000},
1002: {"name": "Bob", "department": "Sales", "salary": 50000},
1003: {"name": "Charlie", "department": "Engineering", "salary": 80000},
1004: {"name": "Dave", "department": "Marketing", "salary": 60000},
1005: {"name": "Eve", "department": "Sales", "salary": 55000}
}
Resulting dictionary:
{
"Engineering": {1001: {"name": "Alice", "salary": 75000}, 1003: {"name":
"Charlie", "salary": 80000}},
"Sales": {1002: {"name": "Bob", "salary": 50000}, 1005: {"name": "Eve",
"salary": 55000}},
"Marketing": {1004: {"name": "Dave", "salary": 60000}}
}