Python - Access Array Items



Accessing an array items in Python refers to the process of retrieving the value stored at a specific index in the given array. Here, index is an numerical value that indicates the location of array items. Thus, you can use this index to access elements of an array in Python.

An array is a container that holds a fix number of items of the same type. Python uses array module to achieve the functionality like an array.

Accessing array items in Python

You can use the following ways to access array items in Python −

  • Using indexing
  • Using iteration
  • Using enumerate() function

Using indexing

The process of accessing elements of an array through the index is known as Indexing. In this process, we simply need to pass the index number inside the index operator []. The index of an array in Python starts with 0 which means you can find its first element at index 0 and the last at one less than the length of given array.

Example

The following example shows how to access elements of an array using indexing.

 import array as arr # creating array numericArray = arr.array('i', [111, 211, 311, 411, 511]) #indexing print (numericArray[0]) print (numericArray[1]) print (numericArray[2]) 

When you run the above code, it will show the following output

 111 211 311 

Using iteration

In this approach, a block of code is executed repeatedely using loops such as for and while. It is used when you want to access array elements one by one.

Example

In the below code, we use the for loop to access all the elements of the specified array.

 import array as arr # creating array numericArray = arr.array('i', [111, 211, 311, 411, 511]) # iteration through for loop for item in numericArray: print(item) 

On executing the above code, it will display the following result −

 111 211 311 411 511 

Using enumerate() function

The enumerate() function can be used to access elements of an array. It accepts an array and an optional starting index as parameter values and returns the array items by iterating.

Example

In the below example, we will see how to use the enumerate() function to access array items.

 import array as arr # creating array numericArray = arr.array('i', [111, 211, 311, 411, 511]) # use of enumerate() function for loc, val in enumerate(numericArray): print(f"Index: {loc}, value: {val}") 

It will produce the following output

 Index: 0, value: 111 Index: 1, value: 211 Index: 2, value: 311 Index: 3, value: 411 Index: 4, value: 511 

Accessing a range of array items in Python

In Python, to access a range of array items, you can use the slicing operation which is performed using index operator [] and colon (:).

This operation is implemented using multiple formats, which are listed below −

  • Use the [:index] format to access elements from beginning to desired range.

  • To access array items from end, use [:-index] format.

  • Use the [index:] format to access array items from specific index number till the end.

  • Use the [start index : end index] to slice the array elements within a range. You can also pass an optional argument after end index to determine the increment between each index.

Example

The following example demonstrates the slicing operation in Python.

 import array as arr # creating array numericArray = arr.array('i', [111, 211, 311, 411, 511]) # slicing operation print (numericArray[2:]) print (numericArray[0:3]) 

On executing the above code, it will display the following result −

 array('i', [311, 411, 511]) array('i', [111, 211, 311]) 
Advertisements