Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Creating dictionary from two sequences
๐ Scenario: You have two lists: one with product names and another with their prices. You want to combine them into a dictionary where each product name is a key and its price is the value.
๐ฏ Goal: Build a dictionary from two lists using Python, matching each product with its price.
๐ What You'll Learn
Create two lists named products and prices with exact values
Create a variable called product_price_dict to hold the dictionary
Use a Python method to combine products and prices into product_price_dict
Print the product_price_dict to show the final dictionary
๐ก Why This Matters
๐ Real World
Combining two related lists into a dictionary is common when working with data like product catalogs, student grades, or contact information.
๐ผ Career
This skill is useful for data processing, web development, and automation tasks where you need to organize paired data efficiently.
Progress0 / 4 steps
1
Create the product and price lists
Create a list called products with these exact values: 'apple', 'banana', 'cherry'. Also create a list called prices with these exact values: 0.99, 0.50, 2.00.
Python
Hint
Use square brackets [] to create lists and separate items with commas.
2
Prepare the dictionary variable
Create an empty dictionary called product_price_dict to store the combined product-price pairs.
Python
Hint
Use curly braces {} to create an empty dictionary.
3
Combine the lists into a dictionary
Use the zip function with products and prices to create product_price_dict by converting the zipped pairs into a dictionary.
Python
Hint
The zip function pairs items from two lists. Then dict() converts those pairs into a dictionary.
4
Print the dictionary
Print the product_price_dict to display the combined dictionary of products and prices.
Python
Hint
Use the print() function to show the dictionary on the screen.
Practice
(1/5)
1. What does the Python function zip() do when used with two sequences?
easy
A. Sorts both sequences in ascending order
B. Adds elements of both sequences together
C. Removes duplicate elements from both sequences
D. Pairs elements from both sequences into tuples
Solution
Step 1: Understand the purpose of zip()
The zip() function pairs elements from two sequences by their positions, creating tuples.
Step 2: Recognize the output format
Each tuple contains one element from each sequence, matched by index.
Final Answer:
Pairs elements from both sequences into tuples -> Option D
Quick Check:
zip() pairs elements = C [OK]
Hint: Remember zip pairs items by position from sequences [OK]
Common Mistakes:
Thinking zip adds or subtracts elements
Assuming zip sorts sequences
Believing zip removes duplicates
2. Which of the following is the correct syntax to create a dictionary from two lists keys and values?
easy
A. dict(zip(keys, values))
B. dict(keys, values)
C. zip(dict(keys), dict(values))
D. dict(keys + values)
Solution
Step 1: Understand dict() and zip() usage
The dict() function can convert an iterable of key-value pairs into a dictionary. zip(keys, values) creates these pairs.
D. The lists have different lengths causing an error
Solution
Step 1: Check list lengths and zip behavior
zip() pairs elements until the shortest list ends, so no error occurs even if lengths differ.
Step 2: Confirm dict() accepts zip object
dict() can convert the zip object to a dictionary without error.
Final Answer:
There is no error; code runs fine -> Option C
Quick Check:
zip truncates to shortest list; dict accepts zip [OK]
Hint: zip stops at shortest list; no error if lengths differ [OK]
Common Mistakes:
Assuming zip requires equal length lists
Thinking dict() can't convert zip
Expecting error due to list length mismatch
5. Given two lists keys = ['name', 'age', 'city'] and values = ['Alice', '', None], which dictionary comprehension correctly creates a dictionary excluding keys with empty or None values?
hard
A. {k: v for k, v in zip(keys, values)}
B. {k: v for k, v in zip(keys, values) if v}
C. {k: v for k, v in zip(keys, values) if v != ''}
D. {k: v for k, v in zip(keys, values) if v is not None}
Solution
Step 1: Understand filtering with dictionary comprehension
We want to exclude keys where values are empty strings or None. The condition if v filters out falsy values like '' and None.
Step 2: Analyze each option's filter
{k: v for k, v in zip(keys, values) if v} uses if v which excludes both '' and None. {k: v for k, v in zip(keys, values) if v is not None} excludes only None, {k: v for k, v in zip(keys, values) if v != ''} excludes only '', and {k: v for k, v in zip(keys, values)} includes all.
Final Answer:
{k: v for k, v in zip(keys, values) if v} -> Option B
Quick Check:
Filter falsy values with if v = D [OK]
Hint: Use if v to exclude empty or None values in comprehension [OK]