|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/python-counter-class/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/python-counter-class/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fpython-counter-class%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=counting-python-counter |
| Unlock This Lesson | https://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 Counter | https://realpython.com/courses/counting-python-counter/ |
| Christopher Trudeau | https://realpython.com/courses/counting-python-counter/#team |
| Recommended Tutorial | https://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 Question | https://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 |
| Contents | https://realpython.com/lessons/python-counter-class/#description |
| Transcript | https://realpython.com/lessons/python-counter-class/#transcript |
| Discussion | https://realpython.com/lessons/python-counter-class/#discussion |
| Reverse Strings in Python: reversed(), Slicing, and More | https://realpython.com/reverse-string-python/ |
| 00:00 | https://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:11 | https://realpython.com/lessons/python-counter-class/#t=11.76 |
| The Counter class is a member of the collections library, and like several of the | https://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:19 | https://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 corresponding | https://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:39 | https://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 counting | https://realpython.com/lessons/python-counter-class/#t=48.08 |
| whatever iterable is passed in. A string, in an iterable context, gives | https://realpython.com/lessons/python-counter-class/#t=51.62 |
| each letter inside of the string, so passing a string to Counter counts | https://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:09 | https://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 list | https://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:18 | https://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:26 | https://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 corresponding | https://realpython.com/lessons/python-counter-class/#t=87.4 |
| counts. Essentially, the data structure | https://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:48 | https://realpython.com/lessons/python-counter-class/#t=108.71 |
| Each named argument is treated as a string with the corresponding value being | https://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:08 | https://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 in | https://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:19 | https://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:33 | https://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 key | https://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:42 | https://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:52 | https://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:04 | https://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:26 | https://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:36 | https://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:48 | https://realpython.com/lessons/python-counter-class/#t=228.33 |
| That’s a lot of 'i's. Notice updating with -5 'h's | https://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:04 | https://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:26 | https://realpython.com/lessons/python-counter-class/#t=266.81 |
| Likewise, counters have a .keys() method so that you can get at the keys in your | https://realpython.com/lessons/python-counter-class/#t=266.81 |
| counter … | https://realpython.com/lessons/python-counter-class/#t=270.86 |
| 04:44 | https://realpython.com/lessons/python-counter-class/#t=284.84 |
| and a .values() method … | https://realpython.com/lessons/python-counter-class/#t=284.84 |
| 04:56 | https://realpython.com/lessons/python-counter-class/#t=296.9 |
| and an .items() method. | https://realpython.com/lessons/python-counter-class/#t=296.9 |
| 05:11 | https://realpython.com/lessons/python-counter-class/#t=311.08 |
| All your inherited dictionary goodness. One behavior of counters | https://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’t | https://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:34 | https://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 one | https://realpython.com/lessons/python-counter-class/#t=335.89 |
| is going to return zero. Another service | https://realpython.com/lessons/python-counter-class/#t=339.28 |
| the counter offers is information on how common the elements in the container | https://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:04 | https://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, tomatoes | https://realpython.com/lessons/python-counter-class/#t=369.96 |
| the least. | https://realpython.com/lessons/python-counter-class/#t=374.76 |
| 06:19 | https://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:36 | https://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 the | https://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:52 | https://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:02 | https://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:19 | https://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 an | https://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:47 | https://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 list | https://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:11 | https://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 Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/counting-without-counter/ |
| Overview | https://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 Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover