Summary
I created this video to demonstrate how to use Python for‑loops and related tools to analyse a music‑listening dataset. I start by showing how the range operator works (range(start, stop) defaults to 0) and why accumulator variables—like an empty string for building a top‑5 song list—must be initialised outside the loop, otherwise they reset each iteration. I compare iterating directly over a list (for item in list) with using indices (for i in range(len(list))) and explain that direct iteration is preferred unless you need parallel access to several lists.
Next, I illustrate two ways to count occurrences: a manual counter and the idiomatic collections.Counter, using Taylor Swift songs as an example. I then compute total listening time by multiplying play count by duration, showing the difference between rounding (round() or format specifiers) and floor‑division (//) for hour totals. To locate the most‑played song I initialise a placeholder with the first entry and update it whenever a larger play count appears.
I move on to nested loops, first refactoring a loop that builds a list of unique artists. I stress that list.append adds the whole string, while using += on a list treats the string as an iterable and appends each character separately. By checking membership before appending, I generate a clean list of unique artists and also identify three distinct genres (pop, R&B, indie). I demonstrate installing the tqdm library with pip install tqdm and show how it adds a progress bar to long loops.
Finally, I outline a nested‑loop algorithm to find the artist with the highest total play count: construct the unique‑artist list, loop through each artist, sum the corresponding play counts via index alignment, and keep track of the maximum. The result is a clear top‑artist ranking, a concise unique‑artist list, and a progress‑bar‑enhanced workflow ready to be applied to any similar dataset. Next steps: install tqdm, replace manual counters with collections.Counter where appropriate, and adapt the presented patterns to your own music or other categorical data analyses.