- Popcorn Hack 1
- Popcorn Hack 2
- Homework Hack #1: Video Notes
- Homework Hack #2: Filter Numbers Divisible by 3 but Not 5
- Homework Hack 3: Develop a function named filter_spotify_data
- Review Questions
Popcorn Hack 1
# Initial list of favorite movies
movies = ["Inception", "Interstellar", "The Dark Knight", "Avengers"]
# Replace the second movie (index 1)
movies[1] = "Everything Everywhere All At Once"
# Add another movie
movies.append("Spider-Man: Into the Spider-Verse")
# Display the updated list
print("Updated Movie List:")
print(movies)
Updated Movie List:
['Inception', 'Everything Everywhere All At Once', 'The Dark Knight', 'Avengers', 'Spider-Man: Into the Spider-Verse']
Popcorn Hack 2
ages = [15, 20, 34, 16, 18, 21, 14, 19]
# Filter for ages 18 or older
voter_eligible = [age for age in ages if age >= 18]
print("Voter Eligible Ages:")
print(voter_eligible)
Voter Eligible Ages:
[20, 34, 18, 21, 19]
Homework Hack #1: Video Notes
🔹 Complete Python List Tutorial
-
Lists are mutable — you can change, add, or remove elements.
-
You can access list items with indexing and slicing.
-
append() adds an item; insert() adds at a specific index.
-
remove() deletes by value; pop() removes by index.
-
List comprehension allows clean one-liner filtering.
-
len() gives list size; sort()/reverse() change order.
🔹 How to Filter Lists in Python
-
List comprehension is the most efficient filtering method.
-
Filtering syntax: [item for item in list if condition].
-
You can filter based on multiple conditions using and/or.
-
Filtering works with strings, numbers, even custom objects.
-
filter() + lambda is an alternative way to filter.
-
Filtering is linear in time complexity: O(n).
-
Always test your filters to confirm accuracy.
Homework Hack #2: Filter Numbers Divisible by 3 but Not 5
# Original list from 1 to 30
numbers = list(range(1, 31))
# Filter numbers divisible by 3 but NOT by 5
filtered = [num for num in numbers if num % 3 == 0 and num % 5 != 0]
print("Original Numbers:")
print(numbers)
print("Filtered Numbers (Divisible by 3 but not 5):")
print(filtered)
Original Numbers:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
Filtered Numbers (Divisible by 3 but not 5):
[3, 6, 9, 12, 18, 21, 24, 27]
Homework Hack 3: Develop a function named filter_spotify_data
import pandas as pd
def filter_spotify_data(file_path):
# Load the Spotify CSV data
df = pd.read_csv(file_path)
# Filter for songs with over 10 million streams in the last 30 days
filtered_df = df[df["Streams Last 30 Days (Millions)"] > 10]
# Sort by most streamed
filtered_df = filtered_df.sort_values(by="Streams Last 30 Days (Millions)", ascending=False)
return filtered_df
# File path to your CSV file
file_path = "Spotify_2024_Global_Streaming_Data.csv"
# Apply the function
filtered_songs = filter_spotify_data(file_path)
# Display the result
print(filtered_songs)
Country Artist Album Genre \
133 Italy Taylor Swift 1989 (Taylor's Version) Indie
73 Brazil Ed Sheeran Autumn Variations Pop
218 Argentina Ariana Grande Eternal Sunshine K-pop
148 India BTS Proof R&B
187 South Korea SZA SOS Pop
.. ... ... ... ...
444 Japan BLACKPINK BORN PINK K-pop
398 Netherlands Taylor Swift 1989 (Taylor's Version) R&B
378 Netherlands Billie Eilish Happier Than Ever Hip Hop
470 India BTS Proof Jazz
340 India BLACKPINK BORN PINK K-pop
Release Year Monthly Listeners (Millions) Total Streams (Millions) \
133 2020 18.85 1110.25
73 2021 73.72 3038.38
218 2020 27.47 2014.64
148 2018 79.32 4434.66
187 2018 30.54 3033.78
.. ... ... ...
444 2021 99.80 2345.24
398 2018 13.66 2982.12
378 2023 68.92 167.58
470 2018 32.61 2846.32
340 2022 62.69 2989.92
Total Hours Streamed (Millions) Avg Stream Duration (Min) Platform Type \
133 4879.92 3.47 Free
73 7753.12 2.96 Free
218 6539.73 3.31 Free
148 13885.77 3.12 Premium
187 13217.91 3.17 Premium
.. ... ... ...
444 9575.26 3.16 Free
398 8258.83 3.53 Free
378 659.22 2.58 Free
470 8976.33 3.90 Free
340 7761.60 2.72 Free
Streams Last 30 Days (Millions) Skip Rate (%)
133 200.00 7.04
73 199.91 27.22
218 198.06 9.81
148 197.84 22.18
187 197.83 22.19
.. ... ...
444 12.15 31.25
398 12.13 11.07
378 11.80 31.97
470 11.05 29.53
340 10.60 5.52
[476 rows x 12 columns]
Review Questions
1. What are lists in Python?
Lists are ordered, changeable (mutable) collections used to store multiple items.
Common operations:
append()→ add item to endremove()→ delete itemmy_list[i] = value→ update itemsort(),reverse()→ organize items
2. Real-world scenario for filtering algorithms
A music app filtering songs based on user preferences like:
- Genre (e.g., pop, jazz)
- Artist
- Rating or popularity
3. Why is filtering efficiency important?
Efficient filtering:
- Saves time and memory
- Speeds up performance with large datasets
- Improves user experience in real-time apps