×

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 - Access List Items

Last Updated : May 01, 2025

In Python list, each element is assigned a position, starting from index 0 for the first item. You can retrieve any item by referring to its index within square brackets and using other approaches also. In this chapter, we will learn different approaches to access list items.

Accessing List Items Using Index

You can access individual items in a list by referring to their index. In Python, indexing starts from 0, so the first item is at index 0, the second at index 1, and so on.

Example

In this example, we are accessing elements of a list using their index positions.

# Creating a list
cities = ["New York", "Los Angeles", "Chicago"]

# Accessing the first item
print(cities[0])

# Accessing the second item
print(cities[1])

The output of the above code would be:

New York
Los Angeles

Accessing List Items Using Negative Indexing

Python supports negative indexing, which allows you to access items from the end of the list. The last item has index -1, the second last has index -2, and so on.

Example

In this example, we are accessing list items using negative indices.

# Creating a list
cities = ["New York", "Los Angeles", "Chicago"]

# Accessing the last item
print(cities[-1])

# Accessing the second last item
print(cities[-2])

The output of the above code would be:

Chicago
Los Angeles

Accessing a Range of Items Using Slicing

You can use slicing to access a range of items in a list. The syntax is list[start:end], where start is the starting index (inclusive) and end is the ending index (exclusive).

Example

In this example, we are accessing a range of list items using slicing.

# Creating a list
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

# Accessing a range of items
print(cities[1:4])

The output of the above code would be:

['Los Angeles', 'Chicago', 'Houston']

Exercise

Select the correct option to complete each statement about accessing items in a Python list.

  1. Which index accesses the first item in a list my_list = [10, 20, 30]?
  2. What does my_list[-1] return for my_list = [5, 10, 15]?
  3. What will my_list[1:3] return for my_list = [100, 200, 300, 400]?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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