Animated Python: Dictionaries and Most Common Elements
Channel: Kevin Lubick
Date: 2023-01-02

This video animates the execution of a short Python program that finds the most common letter in a user-provided sentence. Viewers can see how the computer executes the code step by step and see the contents of the variables change as the computer executes the operations. The key components illustrated are for each loops, dictionaries, lists, conditionals, and variables. The code is broken down into two coding patterns, the "count" pattern and the "find largest" pattern.

Another way to visualize code running is https://pythontutor.com (which also has versions for C++, Java, and other programming languages).

Technical note:
As of Python 3.7, dictionaries will iterate through keys in the same order they were added to the dictionary. Prior to that, the key order was unspecified. (https://stackoverflow.com/a/39980744/1447621)
I chose to make my animated dictionary store the keys in alphabetical order, as I thought that would be more intuitive and similar to a "tree map" data structure.

Playlist of Visualizations:
 - https://www.youtube.com/playlist?list=PLDs5gxU1J5CLKlTjZKLtPau7DFrAnDRHD

Animations made by Kaylee L with Manim Community edition (https://www.manim.community/)
 - https://github.com/kjlubick/manim_ace

Narration by James K. Background music from YouTube Audio Library (https://youtube.com/audiolibrary)
 - Wager With Angels by Nathan Moore
 - Tinker Time by Nathan Moore

0:00 Intro - Finding the most common element
0:33 Execution begins with "pepperoni pizza"
1:09 First for each loop begins iterating over prose string
1:57 Add first key-value pair to the dictionary
3:14 Update existing key-value pair in the dictionary
4:55 End of first for each loop
5:29 Second for each loop begins iteration over char_count dictionary
8:03 Handling a tie for most common letter
9:08 Largest count is found
9:49 End of second for each loop
10:06 Reusable steps are coding patterns