Skip to the content.

Lists and Filtering Algorithm

Lists and Filtering Algorithms

Lists and Filtering Algorithms

What is a List?

College Board Definition: A list is an ordered sequence of elements. It allows multiple related items to be stored in a single variable.

Simplified: A list is like a basket that holds multiple items, all under one name.

Why use lists?

score1 = 87
score2 = 90
...
scores = [87, 90, 85, 70, 80] # Much easier!

Popcorn Hack 1

Question: What are some possible benefits of using lists? What are some real-world examples?
Answer: Lists make it easier to store, access, and manage data.
  • Shopping Cart: Items added are stored in a list.
  • Email Inbox: A list of emails to view and manage.
  • Music Playlist: Each playlist is a list of songs.

Working with Lists

  • Creating: myList = ["Ford", "BMW", 2025]
  • Accessing: myList[0]
  • Modifying: myList[1] = "Tesla"
  • Adding: append(), insert()
  • Removing: remove(), pop()
  • Slicing: list[0:3]

Traversing Lists

Going through a list item by item with a loop.

colors = ["red", "blue", "green"]
for color in colors:
    print("Color:", color)
  • 📧 Check unread emails
  • 🎮 Total up game scores
  • 🛍️ Find cheapest item
  • 📊 Analyze data
grades = [85, 92, 76, 98]
above_90 = []
for grade in grades:
    if grade > 90:
        above_90.append(grade)

Filtering Algorithms

Filtering = Loop + Condition + Store Match

numbers = [3, 8, 12, 5, 7, 14]
even_numbers = []
for num in numbers:
    if num % 2 == 0:
        even_numbers.append(num)
print(even_numbers) # [8, 12, 14]
names = ["Keerthan", "Zafeer", "Hitin"]
search = input("Type part of a name: ").lower()
filtered = []
for name in names:
    if name.lower().startswith(search):
        filtered.append(name)
print("Filtered names:", filtered)

Popcorn Hack 3

Question: What are real-world examples of filtering algorithms?
  • Spotify: Find songs by genre or mood
  • Shopping sites: Filter by price, brand, size
  • Email: Show only unread messages
  • Data analysis: Extract values that meet conditions

Flask Projects + Filtering

In Flask web apps, users input filters (like category, rating, or search term).

Flask code searches the database and returns only matching results to display.

Homework Hacks

1. Create a list + 3 procedures:

tools = ["hammer", "wrench", "screwdriver"] tools.append("drill") # Appends new item tools.insert(1, "pliers") # Inserts at index 1 tools.remove("hammer") # Removes specific item

2. Traversal: Use a for loop to print each tool:

for tool in tools:
    print("Tool:", tool)

3. Filtering with pandas:

  • Start with a list of numbers.
  • Traverse using a for loop.
  • Use a condition (e.g. numbers > 50).
  • Append matches to a new list.
import pandas as pd
df = pd.DataFrame({"ages": [25, 42, 17, 56, 33]})
filtered_df = df[df["ages"] > 30]
print(filtered_df)

Final Reflection

Lists and filtering algorithms are used every day in websites, apps, and software that organize and search through information. They're essential for making tools that help people find exactly what they're looking for.