How to Use count() in Python (with Examples)

How-to basics · Updated November 23, 2026

The count() method in Python tells you how many times a value appears in a list, tuple, or string. For example, my_list.count(5) returns the number of times 5 appears in my_list. This guide covers the syntax, practical examples, common pitfalls, and troubleshooting for using count() effectively.

What You’ll Need

  • Python 3.x installed (any recent version works).
  • A text editor or IDE (like VS Code, PyCharm, or IDLE).
  • Basic familiarity with running Python scripts or using the interactive shell.

Step-by-Step

  1. Understand the basic syntax. The count() method is called on a list, tuple, or string object. The syntax is: object.count(value). For lists and tuples, value must be an exact match (case-sensitive for strings, and type-sensitive for numbers). For strings, value is a substring.
  2. Use count() on a list. Create a list and call count() on it:
    fruits = ['apple', 'banana', 'apple', 'cherry']
    print(fruits.count('apple'))  # Output: 2

    The method returns an integer. If the item is not found, it returns 0.

  3. Use count() on a tuple. Tuples work the same way as lists:
    numbers = (1, 2, 3, 2, 2, 4)
    print(numbers.count(2))  # Output: 3
  4. Use count() on a string. For strings, count() counts non-overlapping occurrences of a substring. For example:
    text = "hello hello world"
    print(text.count("hello"))  # Output: 2
    print(text.count("l"))       # Output: 6

    Note that count() does not count overlapping occurrences: "aaa".count("aa") returns 1, not 2.

  5. Specify start and end indices (optional). For strings, you can limit the search range using count(sub, start, end). The start and end indices follow the same slicing rules (end is exclusive). Example:
    text = "banana"
    print(text.count("a", 2, 5))  # Output: 1  (only looks at 'nan')

    Lists and tuples do not accept start/end arguments.

  6. Use count() with nested lists (carefully). If you have a list of lists, count() checks for exact equality of the entire sublist. For example:
    matrix = [[1,2], [1,2], [3,4]]
    print(matrix.count([1,2]))  # Output: 2
  7. Combine count() with conditions. To count elements that meet a condition, use a generator expression with sum() instead:
    nums = [1, 2, 3, 4, 5]
    count_even = sum(1 for n in nums if n % 2 == 0)
    print(count_even)  # Output: 2

    This is not a direct use of count(), but it’s a common need.

  8. Count occurrences of all unique values. Use collections.Counter to get counts for every element at once:
    from collections import Counter
    fruits = ['apple', 'banana', 'apple']
    print(Counter(fruits))  # Output: Counter({'apple': 2, 'banana': 1})

Common Mistakes

  • Using count() on an integer or other non-iterable. count() is a method of list, tuple, and string objects. Calling 5.count() raises an AttributeError.
  • Expecting count() to count overlapping substrings. For strings, count() only counts non-overlapping matches. If you need overlapping counts, you must implement a manual loop.
  • Using count() on a dictionary. Dictionaries do not have a count() method. To count values in a dictionary, use list(dict.values()).count(value).
  • Forgetting that count() is case-sensitive for strings. "Hello".count("hello") returns 0. Use lower() if you need case-insensitive counting.
  • Assuming count() works on generators or sets. Sets are unordered and do not support count(); generators do not have it either. Convert to a list first if needed.

Tips

  • For large lists, count() is O(n) – it scans the entire list. If you need to count many different values, consider using collections.Counter once instead of calling count() repeatedly.
  • When counting strings, you can use the optional start and end parameters to avoid counting parts of the string you don’t care about, which can be slightly faster.
  • If you need to count elements that match a condition (like numbers greater than 10), use sum(1 for x in list if condition) – it’s clearer and more efficient than using count() with a filter.
  • Remember that count() returns an integer, so you can directly use it in comparisons like if my_list.count(x) > 0: but for existence checks, in is more efficient and idiomatic.
  • For strings, count() is useful for quick frequency checks, but for more complex pattern matching, use re.findall() or str.find() in a loop.

Troubleshooting

  • AttributeError: ‘int’ object has no attribute ‘count’ – You are trying to call count() on a number. Make sure you are calling it on a list, tuple, or string.
  • TypeError: count() takes no arguments (1 given) – This happens when you call count() without any arguments. Always pass the value you want to count.
  • Unexpected count result for strings with overlapping patterns – Remember count() counts non-overlapping occurrences. If you need overlapping, write a loop: sum(1 for i in range(len(s)) if s[i:i+len(sub)] == sub).
  • Count returns 0 even though you see the item – Check for exact type match. For example, [1, 2].count('1') returns 0 because the string ‘1’ is not the integer 1. Also, for strings, ensure case matches.
  • MemoryError or slow performance on huge lists – If the list is extremely large, count() might be slow. Consider using a Counter or a dictionary to store counts in one pass.

Frequently Asked Questions

  • Can I use count() on a dictionary? No, dictionaries don’t have a count() method. To count how many times a value appears among the dictionary’s values, use list(dict.values()).count(value).
  • Does count() work on numpy arrays? No, numpy arrays have a different method: np.count_nonzero(array == value). Or you can use array.tolist().count(value).
  • How do I count case-insensitive occurrences in a string? Convert both the string and the substring to lowercase (or uppercase) before calling count(): text.lower().count(sub.lower()).
  • Is there a way to count multiple different items at once? Yes, use collections.Counter which returns a dictionary-like object with counts for all unique elements.
  • What is the time complexity of count()? It is O(n) for lists and tuples, and O(n*m) for strings in the worst case (where n is the length of the string and m is the length of the substring), though it’s usually faster in practice.
  • Can I use count() on a range object? No, range objects do not have a count() method. Convert to a list first: list(range(10)).count(5).

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.