Skip to content

Python Array Index Vijetha Spark By {Examples}

  • by

Python Array index is commonly used to refer to the index of an element within an array. You can use array indexing to manipulate and access array elements within an array. Python array is a data structure that can store the collection of elements of the same data type. Arrays are ‘0’ based index, meaning array index starts from ‘0’ not ‘1’.

In this article, I will explain the concept of array indexing and using indexing how you can process and access single/multiple elements from an array with examples.

1. Quick Examples of Python Array Indexing

Following is the quick examples of array indexing.

# Quick examples of array indexing

# Example 1: Create an array of integer type
arr1 = arr.array(‘i’, [2, 4, 6, 8])
print(“Array is : “, arr1)

# Access single array element using indexing
print(“First element:”, arr1[0])
print(“Second element:”, arr1[1])

# Example 2: Access multiple array elements using indexing
print(“Specified portion of an array:”, arr1[:3])

# Example 3: Access array elements using negative indexing
print(“First element:”, arr1[-1])
print(“Second element:”, arr1[-3])

# Example 4: Iterate an array using for loop
# get the elements
for i in range(len(arr1)):
print(arr1[i])

# Example 5: Add elements to an array using array indexing
arr1 = arr.array(‘i’, [2, 4, 6, 8])
print(“Given array is : “, arr1)
arr1[1] = 3
arr1[3] = 5

# Example 6: Add elements to array using insert()
arr1.insert(1, 3)

2. Access Python Single Array Element using Indexing

You can access a single element in an array by using its index. Use the index operator [] to access a specified element in an array. The index must be an integer. Let’s create an array using array module of Python and use index [] operator to access the specified element of the given array.

# Import array module
import array as arr

# Create an array of integer type
arr1 = arr.array(‘i’, [2, 4, 6, 8])
print(“Array is :”, arr1 )

# Access array elements using index
print(“First element:”, arr1[0])
print(“Second element:”, arr1[1])

Yields below output.

3. Access Multiple Elements using Array Slicing

Moreover, you can use the slicing operator to access specified range of elements present in the array. Let’s apply the slicing operator over the given array and get the specified selection of an array.

import array as arr
# Create an array using array module
arr1 = arr.array(‘i’, [2, 4, 6, 8])

# Access multiple array elements using indexing
print(“Given array is : “, arr1)
print(“Specified portion of an array:”, arr1[:3])

Yields below output.

4. Access Elements using Negative Array Indexing

You can also use negative indices to access elements from the end of the array, where -1 refers to the last element, -2 refers to the second-to-last element, and so on.

# Import array module
import array as arr

# Create an array of integer type
arr1 = arr.array(‘i’, [2, 4, 6, 8])
print(“Array is : “, arr1)

# Access array elements using negative indexing
print(“First element:”, arr1[-1])
print(“Second element:”, arr1[-3])

# Output:
# Array is : array(‘i’, [2, 4, 6, 8])
# First element: 8
# Second element: 4

5. Get IndexError

The index must be within a range of the array. If you try to access an element of an array using an index out of the range(greater than or equal to the array’s length), you will get the IndexError with a message of array index out of the range. For example,

# Import array module
import array as arr

# Create an array of integer type
arr1 = arr.array(‘i’, [2, 4, 6, 8])
print(“Array is : “, arr1)

# Access array element with invalid index
print(“First element:”, arr1[4])

# Output:
# IndexError: array index out of range

The above code, has returned IndexError, because we accessed the element of an array with the index 4 which is equal to the length of the array. The height index of an array is 1 less than the length of the array.

6. Access Elements of Array using Iteration

You can iterate arrays using for loop to access each element present in the array. Use For loop in Python over the sequence or iterable objects such as lists, sets, strings, tuples, and dictionaries to perform various operations in Python.

# Create an array of integers
arr1 = arr.array(‘i’, [2, 4, 6, 8])
print(“Given array:”, arr1)

# Iterate an array using for loop
# get the elements
for i in range(len(arr1)):
print(arr1[i])

# Output:
# Given array: array(‘i’, [2, 4, 6, 8])
# 2
# 4
# 6
# 8

7. Add Elements to Python Array

Using its index, you can add/change the elements to an array. You can use index [] notation to add/change the elements of an existing array. For example

import array as arr
# Create an array using array module
# Add elements to an array using array indexing
arr1 = arr.array(‘i’, [2, 4, 6, 8])
print(“Given array is : “, arr1)
arr1[1] = 3
arr1[3] = 5
print(“New array:”, arr1)

# Output:
# Given array is : array(‘i’, [2, 4, 6, 8])
# New array: array(‘i’, [2, 3, 6, 5])

You can also use the insert() method to add an element at a specific index of the array. For example, you use the insert() method to add the integer 2 at the index 1 of the array. The existing elements are shifted to the right to make room for the new element.

# Create an array of integers
arr1 = arr.array(‘i’, [2, 3, 4, 6, 8])

# Add elements to array using insert()
arr1.insert(1, 3)
print(numbers)

# Output
# array(‘i’, [0, 2, 4, 6, 8])

8. Conclusion

In this article, I have explained the concept of Python array indexing and using indexing how you can manipulate and access single/multiple elements from an array with examples.

Happy learning!!

 Python Array index is commonly used to refer to the index of an element within an array. You can use array indexing to manipulate and access array elements within an array. Python array is a data structure that can store the collection of elements of the same data type. Arrays are ‘0’ based index, meaning  Read More Python, Python Tutorial, Pending review by admin, Python Array Examples 

Leave a Reply

Your email address will not be published. Required fields are marked *