René's URL Explorer Experiment


Title: Using Python's Counter Class (Video) – Real Python

Open Graph Title: Using Python's Counter Class – Real Python

Description: In the previous lesson, I showed you a counting problem and tooled a solution by hand. In this lesson, I’ll show you how to use Python’s Counter class to make this simpler. The Counter class is a member of the collections library, and like several…

Open Graph Description: In the previous lesson, I showed you a counting problem and tooled a solution by hand. In this lesson, I’ll show you how to use Python’s Counter class to make this simpler. The Counter class is a member of the collections library, and like several…

Opengraph URL: https://realpython.com/lessons/python-counter-class/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Using Python\u0027s Counter Class",
    "description": "In the previous lesson, I showed you a counting problem and tooled a solution by hand. In this lesson, I’ll show you how to use Python’s Counter class to make this simpler. The Counter class is a member of the collections library, and like several…",
    "thumbnailUrl": ["https://files.realpython.com/media/How-to-Use-Counter-in-Pythons-Collections-Module_Watermarked.673c6dc8ee2e.jpg"],
    "uploadDate": "2022-03-08T14:00:00+00:00",
    "duration": "PT8M17S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/python-counter-class/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/How-to-Use-Counter-in-Pythons-Collections-Module_Watermarked.673c6dc8ee2e.jpg
og:imagehttps://files.realpython.com/media/How-to-Use-Counter-in-Pythons-Collections-Module_Watermarked.673c6dc8ee2e.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/python-counter-class/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/lessons/python-counter-class/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Flessons%2Fpython-counter-class%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=counting-python-counter
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=counting-python-counter
https://realpython.com/courses/counting-python-counter/#team
Counting With Python's Counterhttps://realpython.com/courses/counting-python-counter/
Christopher Trudeauhttps://realpython.com/courses/counting-python-counter/#team
Recommended Tutorialhttps://realpython.com/python-counter/
Course Slides (.pdf)https://realpython.com/courses/counting-python-counter/downloads/counting-python-counter-slides/
Sample Code (.zip)https://realpython.com/courses/counting-python-counter/downloads/counting-python-counter-sample-code/
Ask a Questionhttps://realpython.com/lessons/python-counter-class/#discussion
https://realpython.com/feedback/survey/course/counting-python-counter/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/counting-python-counter/disliked/?from=lesson-title
Contentshttps://realpython.com/lessons/python-counter-class/#description
Transcripthttps://realpython.com/lessons/python-counter-class/#transcript
Discussionhttps://realpython.com/lessons/python-counter-class/#discussion
Reverse Strings in Python: reversed(), Slicing, and Morehttps://realpython.com/reverse-string-python/
00:00https://realpython.com/lessons/python-counter-class/#t=0.51
In the previous lesson,https://realpython.com/lessons/python-counter-class/#t=0.51
I showed you a counting problem and tooled a solution by hand. In this lesson,https://realpython.com/lessons/python-counter-class/#t=1.41
I’ll show you how to use Python’s Counter class to make this simpler.https://realpython.com/lessons/python-counter-class/#t=6.27
00:11https://realpython.com/lessons/python-counter-class/#t=11.76
The Counter class is a member of the collections library, and like several of thehttps://realpython.com/lessons/python-counter-class/#t=11.76
other classes within that library, it is based on a dictionary.https://realpython.com/lessons/python-counter-class/#t=15.48
00:19https://realpython.com/lessons/python-counter-class/#t=19.92
In this case,https://realpython.com/lessons/python-counter-class/#t=19.92
the keys are the instances of things being counted while the correspondinghttps://realpython.com/lessons/python-counter-class/#t=20.67
values are the count of those things.https://realpython.com/lessons/python-counter-class/#t=24.84
You came here to code. Let’s dive into the REPL.https://realpython.com/lessons/python-counter-class/#t=29.43
00:39https://realpython.com/lessons/python-counter-class/#t=39.06
Think back to the Mississippi letter-counting problem in the previous lesson.https://realpython.com/lessons/python-counter-class/#t=39.06
Here is a much shorter version.https://realpython.com/lessons/python-counter-class/#t=42.45
Yep. That’s it. Creating a Counter object results in countinghttps://realpython.com/lessons/python-counter-class/#t=48.08
whatever iterable is passed in. A string, in an iterable context, giveshttps://realpython.com/lessons/python-counter-class/#t=51.62
each letter inside of the string, so passing a string to Counter countshttps://realpython.com/lessons/python-counter-class/#t=56.39
each of the unique letters. Counters can also take lists.https://realpython.com/lessons/python-counter-class/#t=60.74
01:09https://realpython.com/lessons/python-counter-class/#t=69.7
Passing in the list, in this case,https://realpython.com/lessons/python-counter-class/#t=69.7
the list is turning the string into the interable of letters, and passing the listhttps://realpython.com/lessons/python-counter-class/#t=71.14
into Counter ends up in the same result.https://realpython.com/lessons/python-counter-class/#t=75.1
01:18https://realpython.com/lessons/python-counter-class/#t=78.61
You can also pass in dictionaries.https://realpython.com/lessons/python-counter-class/#t=78.61
01:26https://realpython.com/lessons/python-counter-class/#t=86.77
In this case,https://realpython.com/lessons/python-counter-class/#t=86.77
the key-value pairs indicate objects being counted and their correspondinghttps://realpython.com/lessons/python-counter-class/#t=87.4
counts. Essentially, the data structurehttps://realpython.com/lessons/python-counter-class/#t=91.18
our results got stored in back when a variety of cats were in danger. All right,https://realpython.com/lessons/python-counter-class/#t=93.85
I’ll stop with the cat jokes,https://realpython.com/lessons/python-counter-class/#t=98.29
I promise. You can also pass in named arguments to Counter.https://realpython.com/lessons/python-counter-class/#t=99.61
01:48https://realpython.com/lessons/python-counter-class/#t=108.71
Each named argument is treated as a string with the corresponding value beinghttps://realpython.com/lessons/python-counter-class/#t=108.71
the count. Alternatively, you can give it a set.https://realpython.com/lessons/python-counter-class/#t=112.52
Remember that sets are also constructed based on iteration,https://realpython.com/lessons/python-counter-class/#t=120.58
but they can only hold one instance of each thing in the iteration.https://realpython.com/lessons/python-counter-class/#t=123.82
02:08https://realpython.com/lessons/python-counter-class/#t=128.14
Creating a set for "mississippi" iterates on the letters,https://realpython.com/lessons/python-counter-class/#t=128.14
keeping only the unique values. Create a counter based on the set results inhttps://realpython.com/lessons/python-counter-class/#t=131.32
a count of 1 for each of the letters.https://realpython.com/lessons/python-counter-class/#t=135.97
02:19https://realpython.com/lessons/python-counter-class/#t=139.54
Counters are just dictionaries.https://realpython.com/lessons/python-counter-class/#t=139.54
You can use any hashable item as a key and store any value.https://realpython.com/lessons/python-counter-class/#t=141.19
The only caveat is that if the value is not an integer,https://realpython.com/lessons/python-counter-class/#t=146.29
you won’t be able to increment that count later.https://realpython.com/lessons/python-counter-class/#t=149.44
02:33https://realpython.com/lessons/python-counter-class/#t=153.07
So although it won’t stop you from storing anything,https://realpython.com/lessons/python-counter-class/#t=153.07
if you want to take advantage of some of the keyhttps://realpython.com/lessons/python-counter-class/#t=155.05
additional features of Counter, you really only want to store integers.https://realpython.com/lessons/python-counter-class/#t=157.06
02:42https://realpython.com/lessons/python-counter-class/#t=162.22
Let me show you another example using arguments.https://realpython.com/lessons/python-counter-class/#t=162.22
02:52https://realpython.com/lessons/python-counter-class/#t=172.53
Here, I’m storing integers, and one of them is negative.https://realpython.com/lessons/python-counter-class/#t=172.53
What does -15 tomatoes mean? Well, maybe you owe your neighbor that many,https://realpython.com/lessons/python-counter-class/#t=176.37
and this counter is tracking what’s in your fridge.https://realpython.com/lessons/python-counter-class/#t=181.06
03:04https://realpython.com/lessons/python-counter-class/#t=184.24
You know how a second ago,https://realpython.com/lessons/python-counter-class/#t=184.24
I mentioned you can increment the counter later? Well it’s later.https://realpython.com/lessons/python-counter-class/#t=185.2
A little deja vu, and one Mississippi.https://realpython.com/lessons/python-counter-class/#t=194.01
And now with the Counter instance,https://realpython.com/lessons/python-counter-class/#t=199.05
I can change values by calling the .update() method.https://realpython.com/lessons/python-counter-class/#t=200.49
03:26https://realpython.com/lessons/python-counter-class/#t=206.47
Like the initializer, .update() takes any iterable. Iterating on "ohio"https://realpython.com/lessons/python-counter-class/#t=206.47
adds two 'o's, an 'h', and an 'i' to the count.https://realpython.com/lessons/python-counter-class/#t=211.18
03:36https://realpython.com/lessons/python-counter-class/#t=216.47
You can see the difference here.https://realpython.com/lessons/python-counter-class/#t=216.47
You can also call .update() with a dictionary.https://realpython.com/lessons/python-counter-class/#t=219.44
03:48https://realpython.com/lessons/python-counter-class/#t=228.33
That’s a lot of 'i's. Notice updating with -5 'h'shttps://realpython.com/lessons/python-counter-class/#t=228.33
leaves -4 'h's in the counter.https://realpython.com/lessons/python-counter-class/#t=233.04
You’re probably starting to see the pattern here. You can update with arguments.https://realpython.com/lessons/python-counter-class/#t=236.34
04:04https://realpython.com/lessons/python-counter-class/#t=244.01
That’s five more 's's and five more 'p's.https://realpython.com/lessons/python-counter-class/#t=244.01
As Counter is just a special type of dictionary,https://realpython.com/lessons/python-counter-class/#t=247.61
you can access a key with a subscript,https://realpython.com/lessons/python-counter-class/#t=249.92
which means you can loop over your counter.https://realpython.com/lessons/python-counter-class/#t=254.23
04:26https://realpython.com/lessons/python-counter-class/#t=266.81
Likewise, counters have a .keys() method so that you can get at the keys in yourhttps://realpython.com/lessons/python-counter-class/#t=266.81
counter …https://realpython.com/lessons/python-counter-class/#t=270.86
04:44https://realpython.com/lessons/python-counter-class/#t=284.84
and a .values() method …https://realpython.com/lessons/python-counter-class/#t=284.84
04:56https://realpython.com/lessons/python-counter-class/#t=296.9
and an .items() method.https://realpython.com/lessons/python-counter-class/#t=296.9
05:11https://realpython.com/lessons/python-counter-class/#t=311.08
All your inherited dictionary goodness. One behavior of countershttps://realpython.com/lessons/python-counter-class/#t=311.08
that’s a little different than dictionaries is when you ask for a key that isn’thttps://realpython.com/lessons/python-counter-class/#t=315.91
there. Instead of getting a KeyError,https://realpython.com/lessons/python-counter-class/#t=319.21
you get the count. There are zero 'a's in mississippi. Well,https://realpython.com/lessons/python-counter-class/#t=323.87
mississippi plus a bunch of other things I did, but there’s no 'a's there.https://realpython.com/lessons/python-counter-class/#t=328.13
05:34https://realpython.com/lessons/python-counter-class/#t=334.36
Note that this is case sensitive.https://realpython.com/lessons/python-counter-class/#t=334.36
Nothing I’ve typed has a capital letter in it. So asking for onehttps://realpython.com/lessons/python-counter-class/#t=335.89
is going to return zero. Another servicehttps://realpython.com/lessons/python-counter-class/#t=339.28
the counter offers is information on how common the elements in the containerhttps://realpython.com/lessons/python-counter-class/#t=343.36
are. Consider a counter tracking how many pieces of fruit got sold.https://realpython.com/lessons/python-counter-class/#t=347.26
06:04https://realpython.com/lessons/python-counter-class/#t=364.44
The .most_common() method returns a list of tuples.https://realpython.com/lessons/python-counter-class/#t=364.44
The tuples are the key-value pairs in the counter,https://realpython.com/lessons/python-counter-class/#t=367.41
and the list is in order of frequency: apples are most common, tomatoeshttps://realpython.com/lessons/python-counter-class/#t=369.96
the least.https://realpython.com/lessons/python-counter-class/#t=374.76
06:19https://realpython.com/lessons/python-counter-class/#t=379.43
You can pass an argument to .most_common() to limit how many items come back.https://realpython.com/lessons/python-counter-class/#t=379.43
This is just the most popular type of fruit.https://realpython.com/lessons/python-counter-class/#t=384.08
And that is the two most popular.https://realpython.com/lessons/python-counter-class/#t=390.1
06:36https://realpython.com/lessons/python-counter-class/#t=396.07
Passing in None is the same as no argument. You get back the whole list.https://realpython.com/lessons/python-counter-class/#t=396.07
Passing in a number bigger than the number of items in the container returns thehttps://realpython.com/lessons/python-counter-class/#t=404.25
whole list as well. What about the least common items?https://realpython.com/lessons/python-counter-class/#t=407.79
06:52https://realpython.com/lessons/python-counter-class/#t=412.53
Well, there’s no method for that, but you can get at it with a bit of work.https://realpython.com/lessons/python-counter-class/#t=412.53
07:02https://realpython.com/lessons/python-counter-class/#t=422.08
I’ll start by capturing the most common. Remember that this returns a list.https://realpython.com/lessons/python-counter-class/#t=422.08
And then I call the .reverse() method on that list.https://realpython.com/lessons/python-counter-class/#t=429.64
Voila! You’ve got it all backwards, as intended.https://realpython.com/lessons/python-counter-class/#t=435.01
07:19https://realpython.com/lessons/python-counter-class/#t=439.36
There’s more than one way to—oh wait, I made a promise. Anyhow,https://realpython.com/lessons/python-counter-class/#t=439.36
you can also call the built-in function reversed().https://realpython.com/lessons/python-counter-class/#t=444.49
This gives you a list_reverseiterator,https://realpython.com/lessons/python-counter-class/#t=452.35
which you can put in a for loop or do whatever else you normally do with anhttps://realpython.com/lessons/python-counter-class/#t=454.96
iterator. I’m just going to stick it in the list so you can see the results.https://realpython.com/lessons/python-counter-class/#t=458.23
07:47https://realpython.com/lessons/python-counter-class/#t=467.81
One last way to accomplish the same thing.https://realpython.com/lessons/python-counter-class/#t=467.81
This is slightly harder to read,https://realpython.com/lessons/python-counter-class/#t=474.48
but a fairly common shortcut. It’s called the negative slice.https://realpython.com/lessons/python-counter-class/#t=476.19
This is a trick that works with the slicing operator ([::-1]) to create a reversed listhttps://realpython.com/lessons/python-counter-class/#t=480.6
in place. See the description below for an article with more details on this.https://realpython.com/lessons/python-counter-class/#t=484.08
08:11https://realpython.com/lessons/python-counter-class/#t=491.04
Now you’ve seen how to use the class. Next up,https://realpython.com/lessons/python-counter-class/#t=491.04
I’ll show you some practical applications.https://realpython.com/lessons/python-counter-class/#t=493.35
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/counting-without-counter/
Overviewhttps://realpython.com/courses/counting-python-counter/
https://realpython.com/lessons/counter-applications-1/
Counting With Python's Counter (Overview) 01:01 https://realpython.com/videos/counting-python-counter-overview/
Counting Without Counter 03:57 https://realpython.com/lessons/counting-without-counter/
Using Python's Counter Class 08:17 https://realpython.com/lessons/python-counter-class/
Exploring Practical Applications: Part 1 06:09 https://realpython.com/lessons/counter-applications-1/
Exploring Practical Applications: Part 2 07:58 https://realpython.com/lessons/counter-applications-2/
Using Counter Instances As Multisets 08:08 https://realpython.com/lessons/counter-multiset/
Counting With Python's Counter (Summary) 01:20 https://realpython.com/lessons/counting-python-counter-summary/
Privacy Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.