×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python - List Indexing

Last Updated : May 01, 2025

In Python, list indexing allows you to access individual elements by their position in the list. In this chapter, we will learn how to use both positive and negative indices to retrieve items with the help of examples.

Accessing List Items Using Positive Index

List indexing starts at 0. You can access elements using their index positions.

Example

In this example, we are accessing specific cities from the list using positive index numbers.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Accessing items using positive index
print(cities[0])  # First item
print(cities[2])  # Third item

The output of the above code would be:

New York
Paris

Accessing List Items Using Negative Index

Negative indexing allows you to access elements from the end of the list. -1 represents the last item, -2 the second last, and so on.

Example

In this example, we are accessing cities using negative indexes.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Accessing items using negative index
print(cities[-1])  # Last item
print(cities[-3])  # Third last item

The output of the above code would be:

Sydney
Paris

List Indexing in a Loop

You can use a loop with range() and len() to access list elements by index.

Example

In this example, we are printing each city name along with its index.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Tokyo", "Sydney"]

# Looping through the list using index
for i in range(len(cities)):
    print(f"Index {i}: {cities[i]}")

The output of the above code would be:

Index 0: New York
Index 1: London
Index 2: Paris
Index 3: Tokyo
Index 4: Sydney

Handling IndexError

Accessing an index that is out of the valid range will raise an IndexError. It is a good practice to check index bounds when necessary.

Example

In this example, we attempt to access an out-of-range index and handle the error using try-except.

# Creating a list of cities
cities = ["New York", "London", "Paris"]

# Trying to access an index that doesn't exist
try:
    print(cities[5])
except IndexError:
    print("Index out of range.")

The output of the above code would be:

Index out of range.

Exercise

Select the correct option to complete each statement about list indexing in Python.

  1. What is the index of the first element in a list in Python?
  2. Given the list my_list = ['apple', 'banana', 'cherry'], what will my_list[1] return?
  3. What will my_list[-1] return if my_list = [10, 20, 30]?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.