#100DaysOfCode in Python Transcripts
Chapter: Days 4-6: Collections module
Lecture: Counter: don't reinvent the wheel

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's move on with Counter. Let's say we have a text which is split into words, and we want to count the most common words.
0:12 Before I knew about collections, I would write something like this. There you go.
0:29 I had to loop over words, keep a dictionary, see if the key was in the dictionary, if not, initialize to zero. If it's in there do plus one.
0:40 Then I had to loop over the items over the key value pairs, sort them and use lambda to sort by value.
0:50 In reversed order and take a slice to get it to five. Now compare that with using Counter, and its most common method. It's like magic, right?
1:03 One line of code, you pass the words list into the Counter, and you call most common and you give the number
1:10 of top words you want to see and compare that with all the work I had to do here and how easy it gets by using the collections, Counter.


Talk Python's Mastodon Michael Kennedy's Mastodon