indexing
In Python, indexing is an operation that allows you to access individual items within a sequence, such as a list, tuple, or string, using integer indices and the syntax sequence[index]
.
Python uses zero-based indexing, meaning that the first item of a sequence has an index of 0
, the second item has an index of 1
, and so on. You can also use negative indices to access items from the end of a sequence, with -1
referring to the last item.
Indexing allows you to retrieve specific items from a sequence. In mutable sequences like lists, it also allows you to modify or delete items.
Example
Here’s a quick example of how you can use indexing in a list:
>>> fruits = ["apple", "banana", "cherry"]
>>> # Accessing items by positive index
>>> fruits[0]
'apple'
>>> fruits[1]
'banana'
>>> # Accessing items by negative index
>>> fruits[-1]
'cherry'
>>> fruits[-2]
'banana'
>>> # Modifying and deleting items
>>> fruits[2] = "orange"
>>> fruits
['apple', 'banana', 'orange']
>>> del fruits[0]
>>> fruits
['banana', 'orange']
Related Resources
Tutorial
Python's list Data Type: A Deep Dive With Examples
In this tutorial, you'll dive deep into Python's lists. You'll learn how to create them, update their content, populate and grow them, and more. Along the way, you'll code practical examples that will help you strengthen your skills with this fundamental data type in Python.
For additional information on related topics, take a look at the following resources:
- Python's tuple Data Type: A Deep Dive With Examples (Tutorial)
- Lists vs Tuples in Python (Tutorial)
- Strings and Character Data in Python (Tutorial)
- Python's del: Remove References From Scopes and Containers (Tutorial)
- Python's Mutable vs Immutable Types: What's the Difference? (Tutorial)
- Exploring Python's tuple Data Type With Examples (Course)
- Lists and Tuples in Python (Course)
- Lists vs Tuples in Python (Quiz)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
- Differences Between Python's Mutable and Immutable Types (Course)
By Leodanis Pozo Ramos • Updated June 3, 2025