Skip to content

Access the Array

In Python, Array are a versatile and commonly used data structure for storing ordered collections of items. Understanding how to access elements within a Array is fundamental to working with this data structure effectively. In this guide, we’ll explore the various methods and techniques for accessing Array items in Python.

Basic Array Access

The most basic way to access items in a Array is to use the index operator [][]. The index operator requires one argument: the index of the item to access. The index of the first item in the Array is 00, the index of the second item is 11, and so on. The following example demonstrates how to access items in a Array using the index operator:

array_indexing.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[0])
print(my_array[1])
print(my_array[2])
print(my_array[3])
array_indexing.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[0])
print(my_array[1])
print(my_array[2])
print(my_array[3])

Output:

command
C:\Users\username>python array_indexing.py
1
2
3
4
command
C:\Users\username>python array_indexing.py
1
2
3
4

In the above example, we create a Array of five elements and print the first four elements using their indices. Note that the last element is not printed because it has an index of 4, which is out of range for the Array.

Negative Array Access

In Python, you can also access Array items using negative indices. Negative indices count backward from the end of the Array. The index of the last item in the Array is -1-1, the index of the second-to-last item is -2-2, and so on. The following example demonstrates how to access items in a Array using negative indices:

array_indexing_negative.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[-1])
print(my_array[-2])
print(my_array[-3])
print(my_array[-4])
array_indexing_negative.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[-1])
print(my_array[-2])
print(my_array[-3])
print(my_array[-4])

Output:

command
C:\Users\username>python array_indexing_negative.py
5
4
3
2
command
C:\Users\username>python array_indexing_negative.py
5
4
3
2

In the above example, we create a Array of five elements and print the last four elements using their negative indices. Note that the first element is not printed because it has an index of -5, which is out of range for the Array.

Diagram of Array Access

The following diagram illustrates how to access items in a Array using positive and negative indices:

array_indexing_diagram.py
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
array_indexing_diagram.py
import array as arr
my_array = arr.array('i', [1, 2, 3, 4, 5])
Element‘a’‘b’‘c’‘d’‘e’
Index01234
Index (Negative)-5-4-3-2-1

Accessing Array Slices

In Python, you can access a slice of a Array using the slice operator [:][:]. The slice operator requires two arguments: the start index and the end index. The slice operator returns a new Array containing the items in the specified range. The following example demonstrates how to access a slice of a Array:

Syntax:

Syntax
my_array[start:end:step]
Syntax
my_array[start:end:step]

Example:

array_slice.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[1:4])
print(my_array[1:4:2])
array_slice.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[1:4])
print(my_array[1:4:2])

Output:

command
C:\Users\username>python array_slice.py
array('i', [2, 3, 4])
array('i', [2, 4])
command
C:\Users\username>python array_slice.py
array('i', [2, 3, 4])
array('i', [2, 4])

In the above example, we create a Array of five elements and print a slice of the Array containing the second, third, and fourth elements. We also print a slice of the Array containing the second and fourth elements.

Omitting the Start Index

In Python, you can omit the start index when accessing a slice of a Array. If you omit the start index, the slice will start at the beginning of the Array. The following example demonstrates how to omit the start index when accessing a slice of a Array:

array_slice_omit_start.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[:4])
print(my_array[:4:2])
array_slice_omit_start.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[:4])
print(my_array[:4:2])

Output:

command
C:\Users\username>python array_slice_omit_start.py
array('i', [1, 2, 3, 4])
array('i', [1, 3])
command
C:\Users\username>python array_slice_omit_start.py
array('i', [1, 2, 3, 4])
array('i', [1, 3])

In the above example, we create a Array of five elements and print a slice of the Array containing the first, second, third, and fourth elements. We also print a slice of the Array containing the first and third elements.

Omitting the End Index

In Python, you can omit the end index when accessing a slice of a Array. If you omit the end index, the slice will end at the end of the Array. The following example demonstrates how to omit the end index when accessing a slice of a Array:

array_slice_omit_end.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[1:])
print(my_array[1::2])
array_slice_omit_end.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[1:])
print(my_array[1::2])

Output:

command
C:\Users\username>python array_slice_omit_end.py
array('i', [2, 3, 4, 5])
array('i', [2, 4])
command
C:\Users\username>python array_slice_omit_end.py
array('i', [2, 3, 4, 5])
array('i', [2, 4])

In the above example, we create a Array of five elements and print a slice of the Array containing the second, third, fourth, and fifth elements. We also print a slice of the Array containing the second and fourth elements.

Omitting the Start and End Indices

In Python, you can omit both the start index and the end index when accessing a slice of a Array. If you omit both indices, the slice will contain all of the items in the Array. The following example demonstrates how to omit both the start index and the end index when accessing a slice of a Array:

array_slice_omit_start_end.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[:])
print(my_array[::2])
array_slice_omit_start_end.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[:])
print(my_array[::2])

Output:

command
C:\Users\username>python array_slice_omit_start_end.py
array('i', [1, 2, 3, 4, 5])
array('i', [1, 3, 5])
command
C:\Users\username>python array_slice_omit_start_end.py
array('i', [1, 2, 3, 4, 5])
array('i', [1, 3, 5])

In the above example, we create a Array of five elements and print a slice of the Array containing all of the elements. We also print a slice of the Array containing the first, third, and fifth elements.

Negative Array Slices

In Python, you can access a slice of a Array using negative indices. Negative indices count backward from the end of the Array. The index of the last item in the Array is -1-1, the index of the second-to-last item is -2-2, and so on. The following example demonstrates how to access a slice of a Array using negative indices:

array_slice_negative.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[-4:-1])
print(my_array[-4:-1:2])
array_slice_negative.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[-4:-1])
print(my_array[-4:-1:2])

Output:

command
C:\Users\username>python array_slice_negative.py
array('i', [2, 3, 4])
array('i', [2, 4])
command
C:\Users\username>python array_slice_negative.py
array('i', [2, 3, 4])
array('i', [2, 4])

In the above example, we create a Array of five elements and print a slice of the Array containing the second, third, and fourth elements. We also print a slice of the Array containing the second and fourth elements.

Negative Array Slices with Omitted Indices

In Python, you can omit the start index and the end index when accessing a slice of a Array using negative indices. If you omit the start index, the slice will start at the beginning of the Array. If you omit the end index, the slice will end at the end of the Array. The following example demonstrates how to omit the start index and the end index when accessing a slice of a Array using negative indices:

array_slice_negative_omit_start_end.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[:-1])
print(my_array[:-1:2])
array_slice_negative_omit_start_end.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[:-1])
print(my_array[:-1:2])

Output:

command
C:\Users\username>python array_slice_negative_omit_start_end.py
array('i', [1, 2, 3, 4])
array('i', [1, 3])
command
C:\Users\username>python array_slice_negative_omit_start_end.py
array('i', [1, 2, 3, 4])
array('i', [1, 3])

In the above example, we create a Array of five elements and print a slice of the Array containing the first, second, third, and fourth elements. We also print a slice of the Array containing the first and third elements.

Array Slice with Negative Step

In Python, you can access a slice of a Array using a negative step. The step is the number of items to skip between successive items in the slice. The following example demonstrates how to access a slice of a Array using a negative step:

array_slice_negative_step.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[4:1:-1])
print(my_array[4:1:-2])
array_slice_negative_step.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(my_array[4:1:-1])
print(my_array[4:1:-2])

Output:

command
C:\Users\username>python array_slice_negative_step.py
array('i', [5, 4, 3])
array('i', [5, 3])
command
C:\Users\username>python array_slice_negative_step.py
array('i', [5, 4, 3])
array('i', [5, 3])

In the above example, we create a Array of five elements and print a slice of the Array containing the fifth, fourth, and third elements. We also print a slice of the Array containing the fifth and third elements.

in Operator

In Python, you can use the inin operator to check if a Array contains a specified item. The inin operator returns TrueTrue if the Array contains the specified item and FalseFalse if it does not. The following example demonstrates how to use the inin operator to check if a Array contains a specified item:

array_in.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(1 in my_array)
print(6 in my_array)
array_in.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(1 in my_array)
print(6 in my_array)

Output:

command
C:\Users\username>python array_in.py
True
False
command
C:\Users\username>python array_in.py
True
False

In the above example, we create a Array of five elements and check if the Array contains the items 11 and 66. The Array contains 11 but not 66, so the first check returns TrueTrue and the second check returns FalseFalse.

not in Operator

In Python, you can use the not innot in operator to check if a Array does not contain a specified item. The not innot in operator returns TrueTrue if the Array does not contain the specified item and FalseFalse if it does. The following example demonstrates how to use the not innot in operator to check if a Array does not contain a specified item:

array_not_in.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(1 not in my_array)
print(6 not in my_array)
array_not_in.py
import array as arr
 
my_array = arr.array('i', [1, 2, 3, 4, 5])
print(1 not in my_array)
print(6 not in my_array)

Output:

command
C:\Users\username>python array_not_in.py
False
True
command
C:\Users\username>python array_not_in.py
False
True

In the above example, we create a Array of five elements and check if the Array does not contain the items 11 and 66. The Array contains 11 but not 66, so the first check returns FalseFalse and the second check returns TrueTrue.

Conclusion

In this guide, we explored the various methods and techniques for accessing Array items in Python. We also explored how to access Array slices in Python. Now that you are familiar with the basics of accessing Array items in Python, you can learn more about Array in Python from the official documentation. For more Python Array tutorials, Check out the Python Central Hub.

Was this page helpful?

Let us know how we did