list.count()

Python · Lists

Theory

.count(value) walks through the whole list and returns how many items are equal (==) to value. For sales = [100, 250, 250, 300], sales.count(250) is 2. A value that does not appear simply gives 0, never an error, so count() is also a safe way to check presence when you need the number anyway.

Because it compares with ==, values that are equal but of different types are counted together: in [1, True, 1.0], count(1) is 3, since True == 1 and 1.0 == 1. Each call scans the entire list, O(n). Calling count() for every distinct value inside a loop therefore becomes O(n squared) and gets slow on large data. To count all values at once, use collections.Counter(sales), which does a single pass and returns a dict-like object of value to count.

Why we use it

Frequency questions come up constantly: how many orders were cancelled, how many times a city appears, are there duplicate IDs? count() answers them for a single value in one line.

When to use it

Use count() for a quick count of one value in a small or medium list. Use collections.Counter or pandas value_counts() when you need counts for every value, and sum(1 for x in items if condition) for counting by a rule.

How it works

  1. Call sales.count(250)
  2. Set a counter to 0
  3. Visit each item in the list
  4. Is the item == 250? Yes: Add 1 to the counter. No: Leave the counter as is.
  5. After the last item, return the counter
  6. print shows 2

Syntax

list.count(value)

Example

sales = [100, 250, 250, 300]
print(sales.count(250))

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