Skip to the content.

BI 3 Lists and Filtering Algorithms

Popcorn Hacks

Popcorn Hack 1

fav_movies = ["Ratatoullie", "The Lion King", "The Incredibles", "Finding Nemo", "Toy Story"]
fav_movies[1] = "The Incredibles 2"
fav_movies.append("Inside Out")
print(fav_movies)
['Ratatoullie', 'The Incredibles 2', 'The Incredibles', 'Finding Nemo', 'Toy Story', 'Inside Out']

Popcorn Hack 2

ages = [15, 20, 34, 16, 18, 21, 14, 19]
eligible = [num for num in ages if num >= 18]
print(eligible)
[20, 34, 18, 21, 19]

Homework Hacks

Homework Hack 1

Complete Python List Tutorial

  • Two ways to create a list: example = list(), or, more commonly used, example = []
  • Can input prepopulated values and add values at the end of the list using append
  • Order is very important for lists
  • Not required to look at whole list to access certain value (order starts with 0, 1, 2, 3,…)
  • To check end of the list, -1 represents end value
  • Slicing is another way to access certain values: primes[2:5], accessing the items starting from 2 but excluding 5
  • To combine two lists, use the + sign: numbers + letters

How to Filter Lists in Python

  • A list comprehension is an easier and more readable way to create a list
  • Best taught using a for loop: I want n for each n in nums –> my_list = [n for n in nums]
  • Map runs everything throughout the function, lambda is anonymous function: compared to list comprehension, less understood by someone completely new to Python
  • Using mod with if statements n for n in nums
  • I want a (letter, num) pair for each letter in ‘abcd’ for each number in ‘0123’: my_list = [(letter, num) for letter in ‘abcd’ for num in range(4)]
  • Zip functions creates tuples that matches up lists for us, can be used for data frame correlation in projects
  • I want a dict{‘name’: ‘hero’} for each name, hero in zip(names, heros) with dictionary comprenehsion: my_dict = {name: hero for name,, hero in zip (names, heros)}

Homework Hack 2

nums = list(range(1, 31))
filtered = [n for n in nums if n % 3 == 0 and n % 5 != 0]

print("Original:", nums)
print("Filtered (div by 3, not by 5):", filtered)
Original: [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 (div by 3, not by 5): [3, 6, 9, 12, 18, 21, 24, 27]

Homework Hack 3

import pandas as pd

# Load the dataset
data = pd.read_csv("Spotify_2024_Global_Streaming_Data.csv")

# Drop rows with missing values
data_cleaned = data.dropna()

# Sort the data by 'Price'
# (If you don't need sorting by 'Price', you can omit this line)
# data_cleaned = data_cleaned.sort_values(by="Price")

# Filter the data for songs with more than 10 million streams
data_filtered = data_cleaned[data_cleaned["Total Streams (Millions)"] > 10]

def filter_spotify_data():
    """
    Displays songs with more than 10 million streams from the cleaned dataset.
    """
    if not data_filtered.empty:
        print("\nSongs with more than 10 million streams:\n")
        print(data_filtered[['Artist', 'Total Streams (Millions)']].to_string(index=False))
        return data_filtered
    else:
        print("No songs found with more than 10 million streams.")
        return pd.DataFrame()  # Return empty DataFrame

# Calling the function to filter and display the songs
filter_spotify_data()

Songs with more than 10 million streams:

        Artist  Total Streams (Millions)
  Taylor Swift                   3695.53
    The Weeknd                   2828.16
   Post Malone                   1425.46
    Ed Sheeran                   2704.33
    Ed Sheeran                   3323.25
 Billie Eilish                   1087.06
     Bad Bunny                   4630.69
      Doja Cat                    377.63
     Bad Bunny                   2828.77
 Ariana Grande                   4312.46
      Dua Lipa                   4310.86
     BLACKPINK                   3624.36
      Doja Cat                   1900.87
           SZA                   1242.31
   Post Malone                   4736.90
       Karol G                   3266.90
    Ed Sheeran                     85.59
 Ariana Grande                   1110.47
    The Weeknd                   1803.77
  Taylor Swift                   4315.28
    The Weeknd                   4287.04
     Bad Bunny                   4330.02
           BTS                   4977.34
    Ed Sheeran                    332.97
         Drake                   1214.26
     BLACKPINK                   4676.80
      Dua Lipa                   3124.74
           SZA                    830.85
           SZA                    579.83
 Ariana Grande                   4238.21
 Billie Eilish                   2780.82
 Ariana Grande                    783.06
 Billie Eilish                   4426.47
   Post Malone                   3695.99
     BLACKPINK                   4092.10
Olivia Rodrigo                   1431.67
     Bad Bunny                   3003.03
           SZA                   3558.49
    Ed Sheeran                   3612.78
     Bad Bunny                   1926.77
           BTS                   3052.97
 Billie Eilish                   3183.06
     Bad Bunny                   2401.92
      Dua Lipa                   3453.73
      Dua Lipa                   4249.26
      Doja Cat                   2761.97
   Post Malone                   1409.26
     Bad Bunny                    806.39
      Doja Cat                   2941.19
   Post Malone                   3686.44
     BLACKPINK                   2051.37
    Ed Sheeran                   3231.61
Olivia Rodrigo                   4758.46
    The Weeknd                    203.60
    Ed Sheeran                   3846.15
         Drake                   2900.19
         Drake                    649.22
 Ariana Grande                   4120.54
 Billie Eilish                   2713.96
           BTS                   2870.76
Olivia Rodrigo                   3000.97
 Ariana Grande                   3573.62
 Billie Eilish                   2678.06
           BTS                   1611.96
       Karol G                   4903.53
     Bad Bunny                   2789.13
      Dua Lipa                   4829.21
    Ed Sheeran                   2836.06
   Post Malone                   1932.87
       Karol G                   1360.00
      Dua Lipa                   3549.43
 Billie Eilish                   1162.81
    Ed Sheeran                   2370.05
    Ed Sheeran                   3038.38
    Ed Sheeran                   3274.97
      Dua Lipa                   3367.16
  Taylor Swift                   1650.21
  Taylor Swift                   3794.39
      Dua Lipa                   3168.61
    The Weeknd                   3256.16
      Dua Lipa                   1948.75
Olivia Rodrigo                   1450.05
Olivia Rodrigo                   1917.19
  Taylor Swift                   4571.48
    Ed Sheeran                   2229.52
         Drake                   3478.89
      Doja Cat                   1212.11
   Post Malone                   1532.36
    The Weeknd                   3972.97
     Bad Bunny                   2324.87
         Drake                   1530.81
      Doja Cat                   2881.02
           BTS                   3902.64
    Ed Sheeran                   3123.41
           BTS                   3675.18
           SZA                   1399.01
   Post Malone                    746.75
         Drake                    661.76
         Drake                   4426.96
           SZA                    741.90
     Bad Bunny                   4749.43
    Ed Sheeran                   1884.12
           SZA                    989.55
  Taylor Swift                    673.32
Olivia Rodrigo                   4299.11
     Bad Bunny                   1227.76
       Karol G                   1842.09
           BTS                    351.09
           SZA                    375.13
      Dua Lipa                   3975.83
Olivia Rodrigo                    520.67
         Drake                    527.99
 Billie Eilish                   3343.44
         Drake                   3300.84
   Post Malone                   4101.21
  Taylor Swift                   1579.47
       Karol G                   1114.91
    Ed Sheeran                   3784.46
     BLACKPINK                    613.40
     BLACKPINK                   1542.03
       Karol G                   2686.28
     BLACKPINK                   4317.44
     Bad Bunny                   4602.86
     BLACKPINK                   1698.69
Olivia Rodrigo                   1317.04
      Dua Lipa                   4624.56
      Dua Lipa                   1548.96
Olivia Rodrigo                   4887.97
      Dua Lipa                   3044.78
       Karol G                    243.75
     BLACKPINK                   3956.67
       Karol G                   2934.56
           BTS                   1675.96
  Taylor Swift                   1110.25
      Doja Cat                   4183.84
     BLACKPINK                   3479.59
      Dua Lipa                   3929.81
      Dua Lipa                   2539.39
  Taylor Swift                    557.82
           SZA                    503.98
 Ariana Grande                    453.95
     Bad Bunny                   2388.65
  Taylor Swift                   2766.58
  Taylor Swift                   3132.91
    Ed Sheeran                   2174.73
Olivia Rodrigo                   3942.74
           BTS                   1756.77
      Doja Cat                   3492.18
           BTS                   4434.66
  Taylor Swift                   3915.17
       Karol G                    974.75
     BLACKPINK                   1640.00
       Karol G                   3647.73
         Drake                   3762.00
Olivia Rodrigo                    809.52
Olivia Rodrigo                   1285.58
           BTS                   4982.01
      Dua Lipa                    484.56
 Ariana Grande                   3573.46
   Post Malone                    180.11
     BLACKPINK                   2076.70
Olivia Rodrigo                    950.72
     Bad Bunny                   1166.08
           BTS                   2690.72
Olivia Rodrigo                    190.45
    Ed Sheeran                   2750.61
      Dua Lipa                   2226.05
     BLACKPINK                    575.61
     BLACKPINK                   4333.27
      Dua Lipa                    495.74
           BTS                   3288.59
   Post Malone                    303.23
           BTS                    686.88
    The Weeknd                    249.26
     Bad Bunny                   1995.13
       Karol G                    132.35
    Ed Sheeran                   3925.37
   Post Malone                   1248.87
     BLACKPINK                   3076.82
Olivia Rodrigo                   4785.59
    Ed Sheeran                   1431.08
Olivia Rodrigo                   4675.70
   Post Malone                   4778.80
           BTS                    590.67
  Taylor Swift                   2388.40
      Dua Lipa                   3247.19
           SZA                    920.15
           SZA                   3033.78
           SZA                   1004.42
       Karol G                   3798.55
     BLACKPINK                   2413.40
           BTS                   1344.69
           SZA                   4584.85
       Karol G                   2768.71
      Doja Cat                   3533.64
    Ed Sheeran                   4279.71
      Dua Lipa                   2841.36
       Karol G                   2147.30
  Taylor Swift                    720.37
      Doja Cat                   4822.51
 Ariana Grande                   4182.37
     BLACKPINK                    817.85
   Post Malone                    549.44
    The Weeknd                   3891.51
    Ed Sheeran                   1222.67
    Ed Sheeran                   2801.46
     BLACKPINK                   3784.36
           SZA                   2111.71
      Doja Cat                   4155.56
       Karol G                   4353.90
       Karol G                   3533.65
 Billie Eilish                   2588.44
Olivia Rodrigo                   4327.22
     BLACKPINK                   3290.31
      Dua Lipa                    887.87
      Doja Cat                   4220.60
    Ed Sheeran                    127.87
  Taylor Swift                   4040.53
 Ariana Grande                   2014.64
 Billie Eilish                   1916.99
    Ed Sheeran                   1187.34
           SZA                    937.80
           SZA                   1240.34
 Ariana Grande                   1199.11
   Post Malone                   2787.54
       Karol G                   1931.00
           BTS                    556.05
         Drake                   4325.40
    Ed Sheeran                    889.03
           BTS                   4723.90
     BLACKPINK                   4317.88
      Dua Lipa                   4845.19
     BLACKPINK                   4663.73
       Karol G                   4164.84
    The Weeknd                   2144.48
 Ariana Grande                   4070.87
     Bad Bunny                   3242.90
         Drake                   2618.05
       Karol G                    171.59
Olivia Rodrigo                   1073.99
           BTS                   4253.44
     Bad Bunny                   2160.53
  Taylor Swift                   2176.29
    Ed Sheeran                   1663.63
     BLACKPINK                   1772.23
Olivia Rodrigo                    956.07
     Bad Bunny                   4599.85
      Doja Cat                   1827.24
     BLACKPINK                   2247.07
           BTS                   3874.11
 Ariana Grande                   1131.78
           BTS                   4970.09
     Bad Bunny                   4076.97
           BTS                   2596.35
           BTS                   1285.90
 Billie Eilish                   3186.46
      Dua Lipa                   3881.97
   Post Malone                   2176.73
      Doja Cat                   3823.16
      Dua Lipa                   2167.61
         Drake                   3339.32
 Billie Eilish                   1106.43
      Doja Cat                    400.60
     Bad Bunny                   3401.83
           SZA                   4306.81
Olivia Rodrigo                    467.03
    The Weeknd                   3751.83
 Billie Eilish                   4889.60
Olivia Rodrigo                   2381.52
   Post Malone                   3577.51
     Bad Bunny                   2580.96
           BTS                   1231.26
 Billie Eilish                    729.20
     Bad Bunny                   4373.63
         Drake                   3156.29
     Bad Bunny                   3386.90
  Taylor Swift                    792.92
           BTS                   2535.10
      Doja Cat                   2787.80
    Ed Sheeran                   2755.47
Olivia Rodrigo                   3703.51
       Karol G                   1195.79
         Drake                   2002.04
           SZA                   1664.95
     Bad Bunny                   1604.46
         Drake                   3538.29
     BLACKPINK                   1337.83
    The Weeknd                   3979.36
 Billie Eilish                   4591.27
         Drake                   2331.92
     Bad Bunny                   1411.16
      Dua Lipa                    729.32
     BLACKPINK                   2449.16
    The Weeknd                   4958.08
           BTS                   4982.14
     Bad Bunny                   3703.62
    The Weeknd                   2477.66
           SZA                   1736.76
   Post Malone                   4804.77
     Bad Bunny                   3969.62
   Post Malone                   4081.55
     BLACKPINK                   1625.17
    The Weeknd                   4432.34
       Karol G                    584.77
           SZA                    552.07
           BTS                    568.39
Olivia Rodrigo                   3537.30
    The Weeknd                   4631.59
 Ariana Grande                     68.89
 Billie Eilish                    621.77
Olivia Rodrigo                    270.85
         Drake                   3794.46
 Billie Eilish                   2407.50
 Billie Eilish                   3798.08
         Drake                   3154.77
Olivia Rodrigo                   4373.66
    Ed Sheeran                   2835.28
    Ed Sheeran                   1413.30
       Karol G                   4608.13
      Doja Cat                   3731.43
    The Weeknd                   2606.68
     Bad Bunny                   1004.18
       Karol G                   2347.88
     Bad Bunny                   1754.03
      Doja Cat                   3254.53
      Dua Lipa                   4014.28
     BLACKPINK                   1130.13
 Ariana Grande                   3437.85
 Billie Eilish                   3839.10
 Billie Eilish                   2121.90
 Billie Eilish                   1535.96
       Karol G                   4502.98
Olivia Rodrigo                   4263.51
      Doja Cat                    317.61
     Bad Bunny                    450.02
    The Weeknd                   2473.41
     BLACKPINK                   3666.76
    The Weeknd                   1948.57
    Ed Sheeran                   3449.56
  Taylor Swift                    699.47
     BLACKPINK                   2989.92
     BLACKPINK                   3524.25
           BTS                   2123.19
    The Weeknd                   4165.92
           SZA                   1686.49
         Drake                    551.39
      Doja Cat                   3399.99
Olivia Rodrigo                   1378.13
           BTS                   4009.50
         Drake                   2866.27
      Dua Lipa                   2231.41
       Karol G                   3365.73
Olivia Rodrigo                   2813.80
      Doja Cat                   1637.14
         Drake                   2268.09
         Drake                   2768.04
Olivia Rodrigo                   4229.37
         Drake                   1739.72
  Taylor Swift                   3598.44
    The Weeknd                    583.44
 Ariana Grande                   3694.26
       Karol G                    685.49
           SZA                   1747.15
           BTS                   4680.78
       Karol G                   3202.54
 Billie Eilish                   2140.81
           BTS                    774.82
         Drake                   4330.97
       Karol G                   4785.67
      Doja Cat                   3537.91
      Dua Lipa                   4814.25
     Bad Bunny                   2358.34
      Doja Cat                   2773.29
     Bad Bunny                    505.13
 Ariana Grande                   4906.61
    The Weeknd                   1937.79
      Doja Cat                   2255.51
     Bad Bunny                    374.09
 Billie Eilish                    167.58
         Drake                   2067.78
  Taylor Swift                   2948.76
         Drake                   4119.51
           BTS                   4575.14
     Bad Bunny                   3003.44
    The Weeknd                    755.15
      Doja Cat                   2770.84
Olivia Rodrigo                    545.22
       Karol G                   1854.24
           BTS                   1691.24
         Drake                   1022.34
      Doja Cat                   2970.68
    The Weeknd                    712.71
 Billie Eilish                    838.15
  Taylor Swift                   1358.57
  Taylor Swift                   2630.80
 Billie Eilish                   4496.22
      Dua Lipa                   4416.07
    Ed Sheeran                   4065.30
  Taylor Swift                   2982.12
 Ariana Grande                   3206.23
      Dua Lipa                   2083.30
       Karol G                    390.77
 Billie Eilish                    928.24
 Ariana Grande                   2634.12
Olivia Rodrigo                   4194.09
           BTS                    985.65
       Karol G                    816.15
   Post Malone                   2809.94
   Post Malone                   4325.96
   Post Malone                   3548.61
           SZA                   1352.31
           BTS                   4110.13
           BTS                   2577.21
         Drake                   2005.96
           SZA                   4086.80
     Bad Bunny                    408.31
           BTS                   3861.34
      Dua Lipa                   3186.63
 Ariana Grande                   1353.58
Olivia Rodrigo                   1381.93
      Doja Cat                   4182.96
    Ed Sheeran                   4247.65
           BTS                    684.82
      Dua Lipa                   1612.47
      Doja Cat                   1223.84
       Karol G                   1434.17
 Ariana Grande                   4010.19
 Ariana Grande                   2832.59
      Doja Cat                   1335.86
      Doja Cat                   3266.83
   Post Malone                   1468.29
 Billie Eilish                   4540.01
    Ed Sheeran                   3915.82
     Bad Bunny                   3732.18
 Ariana Grande                   2643.71
           BTS                   2243.64
      Doja Cat                   2725.89
     BLACKPINK                   1582.82
     Bad Bunny                   4086.13
   Post Malone                   2180.65
     BLACKPINK                   3698.73
   Post Malone                   1459.15
           BTS                   2291.20
  Taylor Swift                    668.94
     BLACKPINK                   2345.24
 Ariana Grande                   4215.62
   Post Malone                    215.88
           SZA                    258.82
           SZA                    609.00
   Post Malone                   2926.33
      Dua Lipa                   4657.70
   Post Malone                   1617.86
  Taylor Swift                   1290.95
     Bad Bunny                   2368.55
      Doja Cat                     53.56
   Post Malone                    363.54
     Bad Bunny                   3222.47
      Doja Cat                    998.50
           BTS                   3603.33
   Post Malone                   4694.31
       Karol G                   1118.71
 Billie Eilish                    935.54
       Karol G                    942.96
           SZA                   1657.04
    Ed Sheeran                   2773.68
           BTS                   4967.39
           SZA                   4633.32
    The Weeknd                    504.93
      Dua Lipa                   1536.95
Olivia Rodrigo                    646.68
           BTS                   2846.32
      Dua Lipa                   1149.32
 Ariana Grande                   2534.02
 Billie Eilish                   4985.54
         Drake                   1858.07
    Ed Sheeran                    272.14
         Drake                   4637.31
      Dua Lipa                   3713.06
       Karol G                   3334.47
  Taylor Swift                    379.66
    The Weeknd                    543.04
 Ariana Grande                   2670.61
 Billie Eilish                   4894.01
 Ariana Grande                   4491.02
     BLACKPINK                   4854.16
 Billie Eilish                   3540.43
Olivia Rodrigo                   3611.23
     BLACKPINK                   4117.41
  Taylor Swift                   2856.86
     Bad Bunny                   3265.36
     BLACKPINK                   1151.64
      Doja Cat                   4340.54
     BLACKPINK                   2703.99
           SZA                   4919.96
   Post Malone                   1507.38
       Karol G                   2947.97
      Dua Lipa                   4418.61
       Karol G                   2642.90
           SZA                   4320.23
           BTS                   4804.15
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

Review Questions

  1. Explain what lists are in Python, including how to modify and manipulate them.
    Lists are datasets organized in either list constructors or brackets, and they are used to display values or strings. To modify and manipulate them, list comprehensions can be performed and using functions such as append, for, and if can help you add or analyze your data.

  2. Provide a real-world scenario where a filtering algorithm might be applied.
    In a media analyzer project, if, for example, we wanted to be provided with data that is filtered together, such as corresponding posted pictures and the acquired number of likes, filtering algorithms can provide these as tuples to be analyzed for ML prediction of how future pictures might perform.

  3. Discuss why analyzing the efficiency of filtering algorithms is important for software development.
    Often times, filtering through exceedingly long lists take an enormous amount of time. Analyzing and enhancing filtering algorithms can be time-saving and effcient when in need of filtered data for certain projects, such as the ML we’re currently working on in class.