Access List Items
3 min read ·
Accessing list items is a core concept in Python.
Because lists are ordered, every item has a fixed position (index) that can be used to read data efficiently.
This topic covers basic to advanced ways of accessing list elements.
List Indexing Basics
List items are accessed using index numbers.
Indexing in Python starts from 0.
Negative Indexing
Negative indexing allows access from the end of the list.
| Index | Meaning |
|---|---|
-1 | Last item |
-2 | Second last item |
Accessing a Range of Items (Slicing)
Slicing allows you to access multiple items at once.
Syntax
startis inclusiveendis exclusive
Slicing with Negative Indexes
Accessing with Step Value
You can control how many items to skip using step.
Syntax
Accessing List Items Using Loop
Using for Loop
Using Index with range()
Accessing Nested List Items
Lists can contain other lists (nested lists).
Accessing List Items Using Conditions
Accessing Items with Unpacking
You can unpack list values into variables.
Extended Unpacking
Index Out of Range Error (Important)
Accessing an index that does not exist raises an error.
Error: