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
Search and Filtering
📖 Scenario: You are organizing a small library of books at home. You want to find books quickly by searching their titles or filtering by genre.
🎯 Goal: Build a simple search and filtering system that helps you find books by title keywords and genre.
📋 What You'll Learn
Create a list of books with title and genre
Add a variable to hold the search keyword
Filter the list of books by the search keyword in the title
Add a filter to select books by genre
💡 Why This Matters
🌍 Real World
Search and filtering help you quickly find information in lists, like books, contacts, or products.
💼 Career
Understanding search and filtering is essential for data handling, user interfaces, and improving user experience in many jobs.
Progress0 / 4 steps
1
Create the list of books
Create a list called books with these exact entries as dictionaries: {'title': 'The Hobbit', 'genre': 'Fantasy'}, {'title': '1984', 'genre': 'Dystopian'}, {'title': 'To Kill a Mockingbird', 'genre': 'Classic'}, {'title': 'The Great Gatsby', 'genre': 'Classic'}, and {'title': 'Brave New World', 'genre': 'Dystopian'}.
No-Code
Hint
Use a list of dictionaries where each dictionary has keys 'title' and 'genre'.
2
Add the search keyword variable
Create a variable called search_keyword and set it to the string 'the' (all lowercase).
No-Code
Hint
Set the variable exactly as search_keyword = 'the'.
3
Filter books by title containing the search keyword
Create a list called filtered_books that contains only the books from books where the search_keyword appears in the book's title (case insensitive). Use a list comprehension with book as the iterator variable.
No-Code
Hint
Use book['title'].lower() to check the title in lowercase.
4
Add genre filtering to the search
Create a variable called selected_genre and set it to 'Classic'. Then update filtered_books to include only books where the title contains search_keyword (case insensitive) and the genre matches selected_genre exactly. Use the same list comprehension style with book as the iterator variable.
No-Code
Hint
Check both conditions with and inside the list comprehension.
Practice
(1/5)
1. What is the main purpose of search in data handling?
easy
A. To find items by matching text or keywords
B. To sort items alphabetically
C. To delete unwanted items
D. To add new items to a list
Solution
Step 1: Understand the meaning of search
Search means looking for something specific by matching text or keywords.
Step 2: Compare options with the definition
Only To find items by matching text or keywords describes finding items by matching text or keywords, which matches the purpose of search.
Final Answer:
To find items by matching text or keywords -> Option A
Quick Check:
Search = find by keywords [OK]
Hint: Search means finding by matching words or text [OK]
Common Mistakes:
Confusing search with sorting
Thinking search adds or deletes items
Mixing search with filtering
2. Which of the following is the correct way to filter a list of fruits to only show those starting with 'A'?
easy
A. Delete fruits not starting with 'A'
B. Sort fruits alphabetically
C. Select all fruits where name starts with 'A'
D. Add fruits starting with 'A' to the list
Solution
Step 1: Understand filtering
Filtering means showing only items that meet a rule, like names starting with 'A'.
Step 2: Match options to filtering
Select all fruits where name starts with 'A' correctly describes selecting items based on a condition. Other options describe sorting, deleting, or adding, which are not filtering.
Final Answer:
Select all fruits where name starts with 'A' -> Option C
Quick Check:
Filter = select by rule [OK]
Hint: Filtering means selecting items by a condition [OK]
Common Mistakes:
Confusing filtering with sorting
Thinking filtering deletes items
Mixing filtering with adding items
3. Given a list of names: ["Anna", "Bob", "Alice", "Mark"], which result shows filtering names starting with 'A'?
medium
A. ["Bob", "Mark"]
B. ["Mark"]
C. ["Anna", "Bob", "Alice"]
D. ["Anna", "Alice"]
Solution
Step 1: Identify names starting with 'A'
From the list, "Anna" and "Alice" start with 'A'.
Step 2: Check options for correct filtered list
["Anna", "Alice"] lists only "Anna" and "Alice", matching the filter condition.
4. You want to filter a list of products by category but the filter shows no results. What could be the problem?
medium
A. The search keyword is too broad
B. The category name used in filter does not match any product
C. The products were deleted
D. The list is sorted incorrectly
Solution
Step 1: Understand filtering by category
Filtering shows items matching the category name exactly.
Step 2: Identify why no results appear
If the category name does not match any product, no items will show. Sorting or deletion are unrelated to filtering results here.
Final Answer:
The category name used in filter does not match any product -> Option B
Quick Check:
Wrong category name = no results [OK]
Hint: Check filter category spelling matches data exactly [OK]
Common Mistakes:
Assuming sorting affects filtering results
Thinking products were deleted without checking
Confusing search keyword with filter category
5. You have a list of books with titles and genres. You want to find all books with 'History' in the title and filter only those in the 'Non-fiction' genre. Which approach is best?
hard
A. First search titles for 'History', then filter results by 'Non-fiction' genre
B. Filter all books by 'Non-fiction' genre, then search titles for 'History'
C. Sort books by title, then filter by genre
D. Search all books for 'History' and ignore genre
Solution
Step 1: Understand combined search and filtering
To find books with 'History' in title and genre 'Non-fiction', both conditions must be applied.
Step 2: Choose the best order
Searching titles first narrows down to relevant books, then filtering by genre further narrows results efficiently. First search titles for 'History', then filter results by 'Non-fiction' genre describes this approach.
Final Answer:
First search titles for 'History', then filter results by 'Non-fiction' genre -> Option A
Quick Check:
Search then filter = best combined approach [OK]
Hint: Search first, then filter for best results [OK]