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
Dictionary Creation
๐ Scenario: You are organizing a small library. You want to keep track of some books and their page counts.
๐ฏ Goal: Create a dictionary to store book titles as keys and their page counts as values. Then display the dictionary.
๐ What You'll Learn
Create a dictionary called books with exact entries
Add a new book entry count variable
Use dictionary comprehension to create a new dictionary with only books having more than 300 pages
Print the new filtered dictionary
๐ก Why This Matters
๐ Real World
Libraries, bookstores, and reading apps often use dictionaries to store book information and filter books based on user preferences.
๐ผ Career
Understanding dictionary creation and filtering is essential for data handling tasks in programming jobs like data analysis, web development, and software engineering.
Progress0 / 4 steps
1
Create the initial dictionary
Create a dictionary called books with these exact entries: 'Python Basics': 250, 'Data Science': 320, 'Machine Learning': 400, 'Deep Learning': 500
Python
Hint
Use curly braces {} to create a dictionary with keys and values separated by colons.
2
Add a page count threshold
Create a variable called min_pages and set it to 300 to use as a filter for books with more pages.
Python
Hint
Just assign the number 300 to the variable min_pages.
3
Filter books with dictionary comprehension
Use dictionary comprehension to create a new dictionary called long_books that contains only books from books with page counts greater than min_pages. Use for title, pages in books.items() in your comprehension.
Python
Hint
Use the format {key: value for key, value in dictionary.items() if condition}.
4
Display the filtered dictionary
Print the long_books dictionary to show only books with more than 300 pages.
Python
Hint
Use print(long_books) to display the dictionary.
Practice
(1/5)
1. Which of the following is the correct way to create an empty dictionary in Python?
easy
A. my_dict = ()
B. my_dict = []
C. my_dict = {}
D. my_dict = ''
Solution
Step 1: Understand dictionary syntax
Dictionaries in Python are created using curly braces {}.
Step 2: Identify the empty dictionary
An empty dictionary is represented as {}, not square brackets, parentheses, or quotes.
Final Answer:
my_dict = {} -> Option C
Quick Check:
Empty dictionary = {} [OK]
Hint: Use curly braces {} for dictionaries, brackets [] for lists [OK]
Common Mistakes:
Using [] which creates a list, not a dictionary
Using () which creates a tuple, not a dictionary
Using '' which creates an empty string
2. Which of the following is the correct syntax to create a dictionary with keys 'a' and 'b' and values 1 and 2 respectively?
easy
A. my_dict = ['a':1, 'b':2]
B. my_dict = {'a'=1, 'b'=2}
C. my_dict = ('a':1, 'b':2)
D. my_dict = {'a':1, 'b':2}
Solution
Step 1: Check dictionary key-value pair syntax
Dictionary pairs use colon : between key and value inside curly braces.
Step 2: Identify correct syntax
my_dict = {'a':1, 'b':2} uses curly braces and colons correctly. Options A and C use wrong brackets, and D uses equals sign which is invalid.
Final Answer:
my_dict = {'a':1, 'b':2} -> Option D
Quick Check:
Dictionary pairs use : inside {} [OK]
Hint: Use colons : between keys and values inside {} [OK]
Common Mistakes:
Using square brackets [] instead of curly braces {}
Trying to access my_dict[4] causes a KeyError because key 4 is not present.
Final Answer:
Key 4 does not exist in the dictionary -> Option A
Quick Check:
Accessing missing key causes KeyError [OK]
Hint: Check if key exists before accessing dictionary [OK]
Common Mistakes:
Assuming all keys exist
Confusing syntax error with runtime error
Thinking keys must be strings
5. You want to create a dictionary from two lists: keys = ['name', 'age', 'city'] and values = ['Alice', 30, 'NY']. Which code correctly creates this dictionary?
hard
A. my_dict = {keys: values}
B. my_dict = {keys[i]: values[i] for i in range(len(keys))}
C. my_dict = dict(keys, values)
D. my_dict = dict(zip(values, keys))
Solution
Step 1: Understand dictionary creation from two lists
We need to pair each key with its corresponding value by index.
Step 2: Analyze options
my_dict = {keys[i]: values[i] for i in range(len(keys))} uses dictionary comprehension with index to pair keys and values correctly. my_dict = dict(keys, values) is invalid syntax. my_dict = {keys: values} creates a dictionary with one key (the list) which is invalid. my_dict = dict(zip(values, keys)) reverses keys and values.
Final Answer:
my_dict = {keys[i]: values[i] for i in range(len(keys))} -> Option B
Quick Check:
Use dict comprehension with index to pair keys and values [OK]
Hint: Use dict comprehension with index or zip() to pair keys and values [OK]