Python Programming - Counter Function

sentdex · Beginner ·🛠️ AI Tools & Apps ·12y ago

Key Takeaways

Uses the Counter function in Python's Collections module to count items in lists

Full Transcript

hello and welcome to a tutorial concerning Python's built-in counter function the purpose of the counter function is to well count it's going to count the number of iterations of every type of thing in a list and then it creates a dictionary of sorts of results in order from greatest to least which contains a key which is going to be the unique item and then the value is going to be how many instances of that item were found in the list so think of this as a way to tally occurrences of items in a list then from there uh you can do all sorts of things so you might just care about you know how many uh instances of something occurred in a list but you can also do things like finding you know the up like X like you can find the uh top three most popular ones or the top five most common occurring ones you you can also add or subtract from other counter lists so let's say you have two lists of a bunch of names and you want to figure out which names are the most common you can do that and then also you can find out um maybe how many more Kelly are in one list than the other list by doing a subtraction function so you can do all kinds of stuff like that so anyway let's go ahead and take a look at uh this module so the first thing that you're going to need to do is import the counter so from collections import counter and that's going to be with a Capital C from there uh we need to make an example list so I'm just going to say uh example list and then that's going to equal uh just some numbers you don't have to copy the same numbers I do I'm just putting in literally random uh numbers well not totally random but just whatever anyway now um to show you the most basic use of counter we can do something like this like counts equals Capital C again don't forget that counter and then in the parameters here you put whatever you want to count like what's the list that you want to count and in our case it's example list so now what we can do is we can print counts and let's go ahead and run that and look at what our output is let me make this a little bit smaller here so this is our output so as you can see uh the most occurring item in our list was a two right and so this is the key right if you're not familiar with the dictionary so two is the key and then eight is how many occurrences or instances of two we had in that list and then the next most common was a one with three and then as you can see we have a next most common and that's a four with three so what who decides on whether it's one or four it's just completely arbitrary I I'm not sure what makes them choose maybe one over three maybe it's like the which one appears first possibly um right so a one appeared in the list before a four so it's probably something like that uh that decides which one comes first but anyway uh from there what it probably is doing is as it sees the number adds one and then is constantly ordering them and so that's probably why number one appeared first but um not positive on that so let's close that out and let's see what some of the other things that we can do so not only can we do this but it's a lot like a dictionary so we can get how many of a specific variable so let's say we wanted to do we want to know how many instances of um three there are in this list so you could say uh like a spe oops specific equals uh counts three and then we can print specific and I'm just going to comment out this other print that we did so we can run this and as you see the output that we get is just one there was only one occurrence of a three but then we can put in I think two is our most popular one so we could run that and we have eight occurrences so that's how you can return um exactly how many of a specific like right we're asking how many twos were in there so I'm going to comment this out and show you guys some more stuff now um what happens if you ask maybe the specific of uh something that doesn't exist like we don't have a 55 so let's say we ask for something that doesn't exist so non-exist equals um oops equals counts 55 now in a normal dictionary doing this would return an error like a key error and but let's see what happens when we try this so print nonexist run that and we return a zero instead as in there's zero instances of that in this list so a little bit different than a dictionary there as far as what happens when you ask for a count of a non-existent item next up though just like any list or dictionary the next thing you can do is redefine an object so um you can do something like um let's say we want counts uh one so this is referring to anything that equals like it's it would be asking like how many instances of one are there and in this case I think we had what three so normally this equals three but then we can we can say nope it doesn't equal three anymore um it equals uh 15 so now what we can do is we can uh print counts one and just make sure and then also let's print just counts everything like the entire dictionary and let's see how it worked so not only did it uh indeed work it changed it to 15 it also changed it in our actual list itself and you'll see how that impacts it later on as well when we do a few more uh commands so let's come down further and I'm going to comment these out again uh that and not only can you um you redefine something to say 15 you could in theory do kind of like a pseudo delete and you could do something like this counts 1 equal 0 now print counts and let's run this list see what happened now as you can see it prints out the list and this time one is no longer 15 it's now equal to zero and it's being included in the list but what if you didn't want it in the list at all well you can also delete it if you want and so let me just comment that out and now we could say um like oops Dell counts one and now we could say print count and run that and now you can see there is no instance of a one right you see some ones here but that's actually how many of that number are there so there is no key of one so that's how you could remove it if you really wanted to remove it or just reassign it if you didn't want to remove it entirely so I'm going to comment these two out again so our counts of one is now equal to 15 again so it's the most popular item in this list again now if you have a counter and you've converted a list into a counter maybe you read a list into memory and then you converted it to a counter and you kind of tossed it out of memory and you're like oh man now I need it back into a list what would you do well you can convert it to a list doing the following so you could say x equals list and then you could say uh counts. elements empty parameters it's a function right and then uh now let's print X run this and now you get a list of these elements and now you'll notice too that it it doesn't it has 15 occurrences of ones now the other thing you'll notice is this is an ordered list uh by uh number of occurrences right so it doesn't return to you the same order that you you know you first fed through but that's a way to turn it into a list of elements now the next thing that we can do is we can count like instead of just taking the returned value because sometimes or probably most of the time when you're counting something you're trying to find maybe the top five most popular items or the top two or sometimes even the top one so how can we do something like that well you can say something like y equals counts. most common and let's say we just want the most common two so print y I'm going to comment out this other print X run it and we can we get a returned it's a list of a list right so we've got the first one where it's the most popular is a one with 50 occurrences so that's that and then we're also return a two with the most or with eight occurrences the second most common and then you can obviously do like y1 if even though you called the most common two you wanted just to know what the first uh first one is and it returns you know a single list and then finally you could go take it even further and oops let's actually get the uh just the number so the most common and actually y1 is the second element so let's get the most common one here so this would return the most common one which is indeed a one so I'll close out of this and let me uh comment this out so that would be a way to get the most common key now the next thing that you can do is actually uh pretty interesting and it's a way where you can add or subtract lists you cannot multiply or divide lists uh sadly so now let's make another list so we're going to make another list literally and I'm just going to put a bunch of random stuff in this one again just like we did with the other one so you don't have to type this out exactly like I did you can just make it random so the next thing that we're going to do is we're going to say counts 2 equals Capital C counter another list done now the next thing that we want to do is let's go ahead and just print counts to make sure we got this far correctly and so again we have this counter and we see that two is now our most popular but also one is the second most popular and so on closing out of that obviously we can perform the same operations that we've been doing but let's do something new so now we're going to say Z equals and what we're going to do with this is we're going to say it equals counts plus counts 2 I'm going to comment this one out so our only return is Z then we're going to print Z so let's see what it gives us now so now as you can see it gives us a combined answer right to our question and again this gives us a counter and in a you know in dictionary like format so we can do all of the same stuff that we've done above um to Z Now um subsequently we can also change this to a subtraction and then still print Z so let's run that and now you can see we've got the list and it's actually a much shorter list so what happened was some of these items uh subtracted so much to the point where uh the result was uh zero or less zero so it's taken out of the list because a counter is meant to tally uh the amount of instances so negative numbers are not uh appreciated by counter so um that's going to conclude my uh tutorial for counter uh I know that was a a lot of stuff to uh throw at you guys as far as the counter um but it's a lot of little basic functions of this massive function but you'll find yourself uh sometimes wanting to do something like like count the most popular stuff and it's a little difficult um to do it all on your own but the counter function really does it and it comes with a lot of these other little functions like the adding and the subtracting and the most common and all this stuff that actually uh is made uh very intuitive on the part of whoever made that function so uh very useful and uh that's going to conclude the tutorial hopefully you guys found it useful as always thanks for watching thanks for your support your subscriptions and until next time

Original Description

This tutorial covers the Counter function that is a part of Python's Collections module. The idea of counter is to... count! I know, shocking. You can use it to easily count tallies of items in lists, find most popular item or items, and even more neat things. Sentdex.com Facebook.com/sentdex Twitter.com/sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →