Computer Science
Grade 8
20 min
Using Lists and Dictionaries Together
Learn how to combine lists and dictionaries to create more complex data structures. Build a simple address book.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Model a collection of complex objects, like a list of users or products, using a list of dictionaries.
Access specific data by chaining index and key lookups (e.g., `my_list[0]['name']`).
Iterate through a list of dictionaries to display or process information for each item.
Create a dictionary where one or more values are lists (e.g., a user profile with a list of friends).
Add new dictionaries to an existing list.
Modify a value within a dictionary that is stored inside a list.
Ever wonder how your phone stores a list of contacts, where each contact has a name, a phone number, AND an email address? 📱 Let's find out how two powerful data structures team up to make it happen!
In this lesson, you'll learn how to combine lists and dict...
2
Key Concepts & Vocabulary
TermDefinitionExample
ListAn ordered, changeable collection of items. You access items by their numerical position (index), starting from 0.`my_pets = ["dog", "cat", "fish"]`
DictionaryAn unordered collection of key-value pairs. You access a value by its unique key, not by a position.`my_pet = {"name": "Fido", "type": "dog"}`
Nested Data StructureA data structure that contains another data structure inside it. For example, a list that contains dictionaries, or a dictionary that contains a list.`users = [ {"name": "Alice"}, {"name": "Bob"} ]` is a list of dictionaries.
List of DictionariesA common pattern where a list is used to hold a sequence of dictionaries, with each dictionary...
3
Core Syntax & Patterns
Accessing a Dictionary in a List
`list_name[index]['key']`
Use this pattern to get a specific value from a dictionary that is inside a list. First, use the square brackets `[index]` to select the dictionary from the list. Then, immediately use `['key']` to get the value associated with that key from the selected dictionary.
Accessing an Item in a List within a Dictionary
`dictionary_name['key'][index]`
Use this pattern when a dictionary's value is a list. First, use `['key']` to access the list. Then, immediately use `[index]` to select the specific item from that list.
Looping Through a List of Dictionaries
`for item in list_of_dictionaries:`
`print(item['key'])`
To process every item in a list of dictionaries...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You need to model a social media feed. Each post has an author, a message, and a list of comments. Each comment also has an author and a message. Which structure best represents a single post?
A.{'author': 'UserA', 'message': 'Hi', 'comments': ['UserB', 'Cool post!']}
B.{'author': 'UserA', 'message': 'Hi', 'comments': [{'author': 'UserB', 'message': 'Cool post!'}]}
C.[{'author': 'UserA', 'message': 'Hi'}, {'author': 'UserB', 'message': 'Cool post!'}]
D.{'post': ['UserA', 'Hi'], 'comments': [{'author': 'UserB', 'message': 'Cool post!'}]}
Challenging
Given a list of products, `products = [{'name': 'Laptop', 'in_stock': True}, {'name': 'Mouse', 'in_stock': False}]`, which code snippet creates a new list containing only the products that are in stock?
A.in_stock_items = []
for p in products:
if p['in_stock'] == True:
in_stock_items.append(p)
B.in_stock_items = []
for p in products:
if p['in_stock'] == True:
in_stock_items = p
C.for p in products:
if p['in_stock'] == False:
products.remove(p)
D.in_stock_items = [p for p in products]
Challenging
You have a shopping cart: `cart = [{'name': 'Apple', 'price': 1.5, 'qty': 2}, {'name': 'Bread', 'price': 3.0, 'qty': 1}]`. Which code correctly calculates the total cost of all items in the cart?
A.total = 0
for item in cart:
total = item['price'] * item['qty']
B.total = 0
for item in cart:
total.append(item['price'] * item['qty'])
C.total = 0
for item in cart:
total += item['price'] * item['qty']
D.total = sum(cart['price'] * cart['qty'])
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free