tuple.count()

Python · Tuples

Theory

.count(value) returns how many items in a tuple are equal (==) to value. With colors = ('red', 'green', 'red'), colors.count('red') is 2. If the value never appears the result is 0, never an error. The comparison is exact equality, so string matching is case-sensitive: 'Red' does not match 'red'.

Tuples have only two methods, count() and index(), because every other list method (append, remove, sort...) would change the sequence, and tuples are immutable. count() does not modify anything; it scans every item once, which is O(n). For counting all distinct values in one pass, collections.Counter(colors) is better than calling count() for each value. Everything else you do with tuples, like len(), in, slicing and sorted(), uses general built-in functions and operators rather than methods.

Why we use it

Tuples often hold fixed sets of responses, survey choices or log records. count() gives a quick frequency for one value without converting the tuple to a list or writing a loop.

When to use it

Use count() for a quick tally of one value in a tuple. Use collections.Counter to count every value, and in when you only need to know whether a value is present at all.

How it works

  1. Call colors.count('red')
  2. Set a counter to 0
  3. Visit each item of the tuple in order
  4. Is the item == 'red'? Yes: Add 1 to the counter. No: Leave it.
  5. Return 2 after the last item; the tuple is unchanged

Syntax

tuple.count(value)

Example

colors = ('red', 'green', 'red')
print(colors.count('red'))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic