Counting in Python

Counting in Python happens via the count() method.

For example, let’s count how many times the word “test” appears in a list:

words = ["test", "test", "not", "a", "test"]

n_test = words.count("test")

print(n_test)

Output:

3

To check how many times a letter occurs in a string, use the count() method of a string.

For example:

num_ls = "Hello world".count("l")

print(num_ls)

Output:

3

These are the basic examples of counting in Python. Let’s next take a deeper look at counting in Python and see more example use cases.

Counting Occurrences of a List Item in Python

Python list has a built-in method count().

It follows the syntax:

list.count(value)

This method loops through the list and counts how many elements match with the value.

For example, let’s count how many times the word "hi" occurs in a list:

words = ["hi", "hi", "hello", "bye"]

n_hi = words.count("hi")

print(n_hi)

Output:

2

Counting Occurrences of a Tuple Item in Python

Python tuple also has a built-in count() method. It works the same way as lists’ count() method.

It follows the syntax:

tuple.count(value)

This method loops through the tuple and counts how many elements match the given value.

For example, let’s count the number of "hello" strings in a list:

words = ("hi", "hi", "hello", "bye")

n_hello = words.count("hello")

print(n_hello)

Output:

1

Counting Occurrences in a String

The count() method counts the number of occurrences of a substring in a larger string.

As a simple example, you can count the number of character 'l's in a string like this:

num_ls = "Hello world".count("l")

print(num_ls)

But there is more to the count() method of a string. The full syntax looks like this:

string_name.count(substring, start_pos, end_pos)

Where

  • substring is the string you want to find out the number of occurrences for.
  • start_pos is the index at which the search begins. This is an optional argument.
  • end_pos is the index at which the search stops. This is also an optional argument.

Let’s see some examples that utilize these.

Counting the Number of Character Matches

This is the most basic usage of the count() method on a string—Figure out the number of times a character occurs in a string.

For example, let’s count the number of character 'e's in a sentence:

num_es = "Hello world".count("e")

print(num_es)

Output:

1

Counting the Occurrences of a Substring

You can also count the number of substring matches (words) in a string.

For example, let’s count many times the substring 'is' occurs in a given sentence:

num_is = "This test is simple".count("is")

print(num_is)

Output:

2

The result is 2 because the substring 'is' is part of the word 'this' and the word 'is' itself.

Counting the Number of Occurrences between Start and End Positions

Finally, let’s see an example of how to use the optional arguments start_pos and end_pos with count().

For example, let’s count the number of words 'is' in the sentence, but let’s ignore the 5 first characters.

num_is = "This test is simple".count("is", 5)

print(num_is)

Output:

1

Even though the word 'is' occurs twice, we get 1 as a result. This is because it also occurs during the first 5 letters which we ignored.

As another example, let’s count the number of words 'is' in the sentence. This time let’s ignore the 4 first characters and the last 9:

num_is = "This test is simple".count("is", 5, 9)

print(num_is)

Output:

0

Even though the word 'is' occurs twice, we get 0 as a result. This is because it occurs during the first 5 letters and the last 9 that are both ignored.

Counting the Number of Occurences of a Value in a Dictionary

A Python dictionary can only have one unique key. Thus, counting the number of keys that equals a value is meaningless, as it is always 0 or 1.

But a dictionary can hold multiple identical values. Thus counting the number of value occurrences is a reasonable task to do.

But there is no special operation for this. Instead, you need to get all the dictionary values as a list and then use the count() method on it.

You can get all the values of a dictionary with the values() method. This returns a special view object you can convert to a list using the list() function.

For example:

data = {
    "age": 5,
    "number_of_siblings": 3,
    "name": "Lisa",
    "address": "Imaginary street 7",
    "favorite_food": "Spaghetti",
    "favorite_number": 5
}

n_fives = list(data.values()).count(5)
print(n_fives)

Output:

2

Conclusion

Today, you learned about counting in Python.

The count() method is a built-in utility for lists, tuples, and strings. It can be used to count how many times a specific item occurs in the sequence.

Thanks for reading. I hope you enjoy it.

Happy coding!

Further Reading

50 Python Interview Questions and Answers

50+ Buzzwords of Web Development

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *