Lists and Filtering Algorithims Popcorn and HW Hacks
Popcorn Hack 1
What are some possible benefits of using lists? What are some real world examples of lists being used in code ?
- Lists are helpful because they let you store and organize multiple pieces of data in one place. Instead of making a separate variable for every item, you can keep them all in a single list and access them by index. Lists make it easier to loop through data, sort items, and perform actions on a group of values. They are also flexible since you can add, remove, or update items at any time. In real-world code, lists are used everywhere. A to-do list app might use a list to store tasks. A streaming service could use lists to keep track of movies in a user’s watch history. In games, a list might store the names and scores of players. Even search engines and social media feeds use lists to manage and display posts or results.
Popcorn Hack 2
items = ["pen", "pencil", "marker", "eraser"]
items.remove("pencil")
items.append("sharpener")
print(items[2])
What does this code output?
-
The original list is:
["pen", "pencil", "marker", "eraser"] items.remove("pencil")- This removes
"pencil"from the list.
Now the list becomes:
["pen", "marker", "eraser"]
- This removes
items.append("sharpener")- This adds
"sharpener"to the end of the list.
Now the list is:
["pen", "marker", "eraser", "sharpener"]
- This adds
print(items[2])- Index 2 means the third item (Python starts at 0).
So the third item is:"eraser"
- Index 2 means the third item (Python starts at 0).
Final answer: eraser
Popcorn Hack 3
What are some real world examples of filtering algorithms?
- Filtering algorithms are used in many real-world situations to sort through large amounts of data and keep only the information that is useful or relevant. One common example is spam filtering in email services, where algorithms check incoming messages for signs of spam like suspicious links or keywords. In recommendation systems, such as those used by Netflix or Spotify, filtering algorithms help suggest movies or songs based on a user’s preferences and past activity. Social media platforms also use filtering to decide which posts appear in a user’s feed, based on who they follow and what content they engage with. In banking, filtering algorithms are used for fraud detection by scanning transactions for unusual patterns that could signal suspicious activity. These algorithms help make systems more efficient and user-friendly by focusing on relevant data.
Homework Hacks
Here is my code below:
################## PART 1 ##################
# Create a list of school supplies
supplies = ["pen", "notebook", "calculator", "highlighter", "ruler"]
# Procedure 1: Add an item to the list using append()
supplies.append("eraser") # Adds "eraser" to the end of the list
# Procedure 2: Remove an item using remove()
supplies.remove("highlighter") # Removes "highlighter" from the list
# Procedure 3: Access an item using an index
print(supplies[2]) # Prints the third item in the list (index 2)
#################### PART 2 ##################
# Steps to go through a list one item at a time:
# 1. Start at the beginning of the list (index 0).
# 2. Use a for loop to move through each element.
# 3. Perform an action with each item (like printing it).
# 4. Stop when all elements have been visited.
# Example of iterating through the list
for item in supplies:
print("Item:", item)
############### PART 3 ##################
# Condition: Keep only items that are longer than 5 letters.
# Steps:
# 1. Start with the original list (supplies).
# 2. Create an empty list called filtered_supplies.
# 3. Use a loop to go through each item.
# 4. If the item has more than 5 letters, add it to the new list.
# 5. At the end, filtered_supplies will only have the items that passed the test.
# Example of filtering the list
filtered_supplies = []
for item in supplies:
if len(item) > 5:
filtered_supplies.append(item)
# Print the filtered list
print("Filtered Supplies:", filtered_supplies)
calculator
Item: pen
Item: notebook
Item: calculator
Item: ruler
Item: eraser
Filtered Supplies: ['notebook', 'calculator', 'eraser']
Final Reflection:
In two complete sentences, explain when and how filtering algorithms and lists are used in real life.
- Filtering algorithms and lists are used in real life whenever we need to search, sort, or organize information based on specific conditions. For example, streaming services use filtering to show movies by genre, rating, or popularity. Online stores use them to display products that match a user’s search or fit within a price range. In coding, we start with a list of data, use a loop to go through each item, apply a condition, and build a new list with only the items that match. This helps make large amounts of information easier to manage and more useful.