List Filtering
List Filtering Blog
- Popcorn Hack 1: What are some possible benefits of using lists? What are some real world examples?
- Popcorn Hack 2: What does this code output?
- Popcorn Hack 3: What are some real world examples of filtering algorithms?
- Homework Hack Part 1: Create a List & Use List Procedures
- Homework Hack Part 2: List Traversal Instructions
- Homework Hack Part 3: Filtering Algorithm
- Final Reflection
Popcorn Hack 1: What are some possible benefits of using lists? What are some real world examples?
Benefits of Lists:
- Store multiple values in a single variable
- Easily loop through, modify, and organize data
- Make code cleaner and more efficient
Real World Examples:
- Shopping Cart – Stores all items you want to purchase
- Inbox – A list of all emails you receive
- Playlist – Songs stored in a list so they can play in order
Popcorn Hack 2: What does this code output?
items = ["pen", "pencil", "marker", "eraser"]
items.remove("pencil")
items.append("sharpener")
print(items[2])
eraser
Popcorn Hack 3: What are some real world examples of filtering algorithms?
Examples of Filtering Algorithms in Real Life:
- Email Inbox – Only show unread or important emails
- Online Stores (Amazon) – Filter by price, brand, or ratings
- Netflix – Recommend shows in a specific genre or rating
- Spotify Search – Finds songs that match keywords
Relation to Flask Projects:
- Flask filters database results based on user input (e.g. SQL queries)
- Matching data is returned using
jsonify()
for frontend display
Homework Hack Part 1: Create a List & Use List Procedures
# My Favorite Fruits List
fruits = ["apple", "banana", "cherry", "kiwi", "grape"]
# append() adds an item to the end of the list
fruits.append("mango") # Adds 'mango'
# insert() adds an item at a specific index
fruits.insert(2, "orange") # Inserts 'orange' at index 2
# remove() removes a specific item
fruits.remove("banana") # Removes 'banana'
print("Final Fruit List:", fruits)
Final Fruit List: ['apple', 'orange', 'cherry', 'kiwi', 'grape', 'mango']
Homework Hack Part 2: List Traversal Instructions
Steps to Traverse a List:
- Start with a list of items.
- Use a
for
loop to go through each item one at a time. - Perform an action (e.g. print or check) during each iteration.
# List of fruits
fruits = ["apple", "orange", "cherry", "kiwi", "grape"]
# Traverse the list using a for loop
for fruit in fruits:
print("Fruit:", fruit)
Fruit: apple
Fruit: orange
Fruit: cherry
Fruit: kiwi
Fruit: grape
Homework Hack Part 3: Filtering Algorithm
Goal: Filter fruits that start with ‘a’ or ‘g’
Steps:
- Start with a list of fruits.
- Traverse the list using a
for
loop. - Use an
if
statement to check if the fruit starts with ‘a’ or ‘g’. - Append matching items to a new list.
# Filter fruits that start with 'a' or 'g'
fruits = ["apple", "orange", "cherry", "kiwi", "grape", "mango"]
filtered_fruits = []
for fruit in fruits:
if fruit.startswith("a") or fruit.startswith("g"):
filtered_fruits.append(fruit)
print("Filtered Fruits:", filtered_fruits)
Filtered Fruits: ['apple', 'grape']
Final Reflection
Filtering algorithms and lists are used every day in apps like Spotify and Netflix to help users find exactly what they want. They work by going through large amounts of data and pulling out only the parts that match the user’s needs.