GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python Array Indexing - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python Array Indexing

Last Updated : 1 Jul, 2026

Array indexing is used to access, retrieve, or modify individual elements stored in an array. Each element is identified by its position, known as an index, which allows direct access to the required value.

Accessing Elements with Indexes

Indexing is the most common way to retrieve values from an array. Each element has a position called an index, which allows you to access it directly without traversing the entire array.

Python
import array

arr = array.array('i', [10, 20, 30, 40, 50])
print(arr[0])
print(arr[1])
print(arr[4])

Output
10
20
50

Explanation:

  • arr[0] accesses the element at index 0, which is 10.
  • arr[1] accesses the element at index 1, which is 20.
  • arr[4] accesses the element at index 4, which is 50.
  • Python arrays use zero-based indexing, so counting starts from 0.

Negative Indexing

Negative indexing allows you to access elements from the end of an array. It is useful when you need the last few elements and do not want to calculate the array length.

Python
import array

arr = array.array('i', [10, 20, 30, 40, 50])
print(arr[-1])
print(arr[-2])

Output
50
40

Explanation:

  • arr[-1] returns the last element of the array.
  • arr[-2] returns the second-last element.
  • Negative indexes start from the end, where -1 represents the last position.

Indexing in 2D Arrays

In a 2D array, data is organized in rows and columns. To access a specific value, you need to specify both the row index and the column index.

Python
matrix = [[1, 2, 3],
          [4, 5, 6],
          [7, 8, 9]]

print(matrix[0][0])
print(matrix[1][2])
print(matrix[2][1])

Output
1
6
8

Explanation:

  • matrix[0][0] accesses the first element of the first row.
  • matrix[1][2] accesses the third element of the second row.
  • matrix[2][1] accesses the second element of the third row.
  • The first index selects the row, while the second index selects the column.

Indexing in 3D Arrays

A 3D array contains multiple 2D arrays. Accessing an element requires three indexes: one for the layer, one for the row, and one for the column.

Python
arr = [[[1, 2, 3], [4, 5, 6]],
       [[10, 11, 12], [13, 14, 15]]]

print(arr[0][0][0])
print(arr[1][1][1])
print(arr[1][0][2])

Output
1
14
12

Explanation:

  • arr[0][0][0] accesses the first element of the first row in the first layer.
  • arr[1][1][1] accesses the second element of the second row in the second layer.
  • arr[1][0][2] accesses the third element of the first row in the second layer.
  • Each additional dimension requires one more index.

Modifying Elements Using Indexes

Indexes can also be used to update existing values in an array.

Python
import array
arr = array.array('i', [10, 20, 30, 40])
arr[2] = 100
print(arr)

Output
array('i', [10, 20, 100, 40])

Explanation:

  • arr[2] refers to the element at index 2.
  • Assigning a new value replaces the existing value.
  • Only the specified element is modified.

IndexError in Arrays

Accessing an index that does not exist raises an IndexError.

Python
import array
arr = array.array('i', [10, 20, 30])
print(arr[5])

Output

ERROR!
Traceback (most recent call last):
File "<main.py>", line 3, in <module>
IndexError: array index out of range

Explanation:

  • The array contains only three elements.
  • Valid indexes are 0, 1 and 2.
  • Accessing index 5 causes Python to raise an IndexError.

Array Slicing

Slicing allows you to extract multiple elements from an array using a range of indexes. It is useful when working with subsets of data.

Python
import array

arr = array.array('i', [10, 20, 30, 40, 50, 60, 70])
print(arr[1:5])
print(arr[::2])
print(arr[-4:-1])

Output
array('i', [20, 30, 40, 50])
array('i', [10, 30, 50, 70])
array('i', [40, 50, 60])

Explanation:

  • arr[1:5] returns elements from index 1 to 4.
  • arr[::2] returns every second element from the array.
  • arr[-4:-1] returns elements from the fourth-last position up to (but not including) the last position.
  • Slicing follows the format array[start:end:step].
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.