In Python, the count method returns the count of how many times an object occurs in a list. The syntax for the count method is really straightforward:
The above example represents the basic syntax for this method. When you’re using it in context, you’ll need to replace the ‘list’ keyword with the actual name of the list containing your objects, and the ‘obj’ keyword with the actual object that you want to be counted. Check out the example below to see a realistic example of how you would use the count method:
First, start with a list:
myList = ['blue', 'orange', 'purple', 'yellow', 'orange', 'green', 'pink'];
Now to count the items in the list using the count method, your code would have to look like this:
As you can probably guess, when you run the above code, the outputs will be 2 and 1, respectively.