Lists and Filtering Algorithms Homework
Homework Hacks and Popcorn Hacks
Popcorn Hacks
Popcorn Hack 1
movies = ["The Matrix", "Captain Marvel", "The Hunger Games", "Harry Potter and the Goblet of Fire"]
movies[1] = "Interstellar"
movies.append("Inside Out")
print("Updated movie list:", movies)
Updated movie list: ['The Matrix', 'Interstellar', 'The Hunger Games', 'Harry Potter and the Goblet of Fire', 'Inside Out']
Popcorn Hack 2
ages = [15, 20, 34, 16, 18, 21, 14, 19]
ages.sort()
eligible_to_vote = [age for age in ages if age >= 18]
print("Sorted ages eligible for voting:", eligible_to_vote)
Sorted ages eligible for voting: [18, 19, 20, 21, 34]
Homework Hack
Homework Hack 1
Video Notes:
#1:
- Python lists can store multiple items, even of different data types (numbers, strings, etc.)
- Can create a list using square brackets, like my_list = [1, 2, 3]
- Lists are ordered, so can access items by their index—starting from 0. Negative indexing counts from the end
- Lists are mutable, which means I can change, add, or remove elements anytime
- Methods like .append(), .insert(), .remove(), and .sort() make it super easy to manipulate lists
- Can slice a list using [start:stop] to get a portion of it without touching the original
- Nested lists (lists inside lists) are possible, and I can access inner elements with double indexing like list[0][1]
#2: Python Comprehensions
- List comprehensions offer a concise way to create lists by embedding a for loop directly within square brackets.
- Can include conditional logic, allowing for filtering elements during list creation.
- Set comprehensions use curly braces {} and are similar to list comprehensions but automatically eliminate duplicate elements.
- Dictionary comprehensions allow for constructing dictionaries using a syntax like {key: value for item in iterable}.
- Comprehensions can be nested, enabling the creation of complex data structures in a readable manner.
- Using comprehensions can lead to more efficient and readable code compared to traditional loops.
- Use comprehensions sparingly to maintain code clarity
Homework Hack 2
numbers = list(range(1, 31))
filtered_numbers = [num for num in numbers if num % 3 == 0 and num % 5 != 0]
print("Original List:")
print(numbers)
print("\nFiltered List (Divisible by 3 but not by 5):")
print(filtered_numbers)
Original List:
[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 List (Divisible by 3 but not by 5):
[3, 6, 9, 12, 18, 21, 24, 27]
Homework Hack 3
import pandas as pd
def filter_spotify_data(file_path):
data = pd.read_csv(file_path)
# Filter for songs with more than 10 million total streams
filtered = data[data['Total Streams (Millions)'] > 10]
print("🎧 Songs with Over 10 Million Total Streams:\n")
return filtered
filter_spotify_data("Spotify_2024_Global_Streaming_Data.csv")
🎧 Songs with Over 10 Million Total Streams:
Country | Artist | Album | Genre | Release Year | Monthly Listeners (Millions) | Total Streams (Millions) | Total Hours Streamed (Millions) | Avg Stream Duration (Min) | Platform Type | Streams Last 30 Days (Millions) | Skip Rate (%) | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | Germany | Taylor Swift | 1989 (Taylor's Version) | K-pop | 2019 | 23.10 | 3695.53 | 14240.35 | 4.28 | Free | 118.51 | 2.24 |
1 | Brazil | The Weeknd | After Hours | R&B | 2022 | 60.60 | 2828.16 | 11120.44 | 3.90 | Premium | 44.87 | 23.98 |
2 | United States | Post Malone | Austin | Reggaeton | 2023 | 42.84 | 1425.46 | 4177.49 | 4.03 | Free | 19.46 | 4.77 |
3 | Italy | Ed Sheeran | Autumn Variations | K-pop | 2018 | 73.24 | 2704.33 | 12024.08 | 3.26 | Premium | 166.05 | 25.12 |
4 | Italy | Ed Sheeran | Autumn Variations | R&B | 2023 | 7.89 | 3323.25 | 13446.32 | 4.47 | Free | 173.43 | 15.82 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
495 | Brazil | Karol G | MAÑANA SERÁ BONITO | Jazz | 2018 | 18.80 | 2947.97 | 12642.83 | 3.59 | Premium | 83.30 | 18.58 |
496 | Canada | Dua Lipa | Future Nostalgia | Classical | 2023 | 89.68 | 4418.61 | 11843.46 | 3.15 | Free | 143.96 | 5.82 |
497 | Germany | Karol G | MAÑANA SERÁ BONITO | Rock | 2023 | 36.93 | 2642.90 | 8637.46 | 4.08 | Free | 76.36 | 15.84 |
498 | Canada | SZA | SOS | Indie | 2022 | 87.26 | 4320.23 | 12201.40 | 2.79 | Free | 84.50 | 13.07 |
499 | Sweden | BTS | Proof | Reggaeton | 2018 | 89.96 | 4804.15 | 12044.32 | 4.03 | Free | 92.27 | 34.36 |
500 rows × 12 columns