list.index()

Python · Lists

Theory

.index(value) scans the list from the start and returns the position of the first item equal to value. For sales = [100, 250, 300], sales.index(250) is 1. If the value appears more than once, only the first position is returned. Optional start and stop arguments limit the search to a slice, so sales.index(250, 2) begins searching at index 2 and finds the second 250 in [100, 250, 300, 250].

If the value is not present, index() raises ValueError rather than returning -1 (strings have .find() for that, lists do not). So either check with if value in sales first, or wrap the call in try/except ValueError. The search is linear, O(n), so calling index() repeatedly inside a loop on a big list gets slow. To find every position, use [i for i, v in enumerate(sales) if v == 250], and if you need fast lookups by value, build a dict that maps values to positions.

Why we use it

Sometimes you know a value and need its position: to update the item next to it, to slice from there, or to report where something first appeared. index() gives you that position directly.

When to use it

Use index() for a one-off 'where is it?' when the value surely exists. Use enumerate() with a comprehension to get all positions, in for a yes/no check, and a dict when you look up many values by key.

How it works

  1. Call sales.index(250)
  2. Start at index 0 (or the given start)
  3. Is the item at this index == 250? Yes: Return this index immediately. No: Move to the next index.
  4. Passed the end (or stop) without a match? Yes: Raise ValueError. No: Keep scanning.
  5. print shows 1

Syntax

list.index(value)

Example

sales = [100, 250, 300]
print(sales.index(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