tuple.index()

Python · Tuples

Theory

.index(value) returns the position of the first item in a tuple that equals value. With colors = ('red', 'green', 'blue'), colors.index('green') is 1. Like the list version it accepts optional start and stop arguments to search only part of the tuple, and it raises ValueError (tuple.index(x): x not in tuple) if the value is missing, so check with in first or catch the error.

index() is for when you know the value and need its position. When you already know the structure of a tuple, which is the normal case for fixed records, unpacking is clearer than indexing: name, dept, salary = row gives each field a name instead of remembering row[2] is the salary. The number of names must match the number of items, otherwise Python raises ValueError; use *rest to collect extras, as in first, *rest = colors.

Why we use it

Fixed lookups, like finding where a weekday or a status sits in an ordered tuple of options, need the position of a known value. index() returns it without converting the tuple or writing a loop.

When to use it

Use index() to find the position of a known value. Use unpacking (a, b, c = t) to read the fields of a record, and a dict when you look up many values by name, since index() scans in O(n).

How it works

  1. Call colors.index('green')
  2. Start scanning at index 0 (or the given start)
  3. Is this item == 'green'? Yes: Return its index. No: Move to the next item.
  4. Reached the end with no match? Yes: Raise ValueError. No: Keep scanning.
  5. print shows 1

Syntax

tuple.index(value)

Example

colors = ('red', 'green', 'blue')
print(colors.index('green'))

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