Python Array Slicing with Examples

Python Array Slicing:
In Python, array slicing refers to accessing sub-parts of an array. These sub-parts can be saved in other variables and altered further.

In addition to the regular slicing notion, arrays in Python can be sliced using the built-in slice() method.

Slicing an array can be done in 2 ways:

Array slicing is supported in Python. It is the generation of a new sub-array from the supplied array based on the user-specified starting and ending indices.

Syntax:

array[start, stop, step]

slice() function:

Python also has a slice() function that returns a slice object with the indices to be sliced.

Syntax:

array[slice(start, stop, step)]

Parameters (applicable for both the ways)

start: It is the starting index from which we must slice the array. 0 is the default index.

stop: It is the ending index before which the slicing procedure would be terminated. By default, this value is equal to the length of the array.

step: step denotes the number of steps required to complete the slicing procedure from start to stop. Set to 1 by default.

Python Array Slicing

1)Passing only one argument

Approach:

  • Import array module using the import Keyword.
  • Import numpy module as np using the import Keyword.
  • Call the array function from array module and initialize with some random values
  • Store it in a variable.
  • Call the array function from numpy module and initialize with some random values.
  • Store it in another variable.
  • Do the slicing by passing the ending index(stop value) and print it.
  • Here start=0 and step = 1 are taken by default
  • The Exit of the Program.

Below is the implementation:

# Import array module using the import Keyword
import array
# Import numpy module as np using the import Keyword
import numpy as np
# Call the array function from array module and initialize with some random values
# Store it in a variable.
arraymodle_arry = array.array('i', [10, 11, 12, 13, 14, 15])
# Call the array function from numpy module and initialize with some random values
# Store it in another variable.
numpy_arry = np.array([21, 22, 12, 23, 25, 9])

# Do the slicing by passing the ending index(stop value) and print it
# Here start=0 and step = 1 are taken by default
print("The result array after slicing using the array module = ",
      arraymodle_arry[:4])
print("The result array after slicing using the Numpy module = ",
      numpy_arry[:5])

Output:

The result array after slicing using the array module = array('i', [10, 11, 12, 13]) 
The result array after slicing using the Numpy module = [21 22 12 23 25]

In this case, we’ve created two arrays, one from the array module and the other from a NumPy array. The output shows the outcomes of slicing both of them using one parameter. As we can see, start and step are set to 0 and 1 by default in both circumstances. The sliced arrays contain entries with indexes ranging from 0 to (stop-1). This is one of the quickest array slicing algorithms in Python.

2)Passing Two arguments

# Import array module using the import Keyword
import array
# Import numpy module as np using the import Keyword
import numpy as np
# Call the array function from array module and initialize with some random values
# Store it in a variable.
arraymodle_arry = array.array('i', [10, 11, 12, 13, 14, 15])
# Call the array function from numpy module and initialize with some random values
# Store it in another variable.
numpy_arry = np.array([21, 22, 12, 23, 25, 9])

# Do the slicing by passing the starting and ending index(start, stop values) and print it
# Here step = 1 is taken by default
print("The result array after slicing using the array module = ",
      arraymodle_arry[3:5])
print("The result array after slicing using the Numpy module = ",
      numpy_arry[2:4])

Output:

The result array after slicing using the array module = array('i', [13, 14]) 
The result array after slicing using the Numpy module = [12 23]

Here the sliced array module array and NumPy array both have elements with indices ranging from start to (stop-1) and a step of 1. As a result, the output is justified.

3)Passing Three arguments

# Import array module using the import Keyword
import array
# Import numpy module as np using the import Keyword
import numpy as np
# Call the array function from array module and initialize with some random values
# Store it in a variable.
arraymodle_arry = array.array('i', [10, 11, 12, 13, 14, 15])
# Call the array function from numpy module and initialize with some random values
# Store it in another variable.
numpy_arry = np.array([21, 22, 12, 23, 25, 9])

# Do the slicing by passing the start, stop and and step index values and print it
# Here start= 1, stop=5 and step=2
print("The result array after slicing using the array module = ",
      arraymodle_arry[1:5:2])
# Here start= 2, stop=6 and step=1
print("The result array after slicing using the Numpy module = ",
      numpy_arry[2:6:1])

Output:

The result array after slicing using the array module = array('i', [11, 13]) 
The result array after slicing using the Numpy module = [12 23 25 9]

Array Slicing using the slice() Method

Approach:

  • Import array module using the import Keyword.
  • Import numpy module as np using the import Keyword.
  • Call the array function from array module and initialize with some random values
  • Store it in a variable.
  • Call the array function from numpy module and initialize with some random values.
  • Store it in another variable.
  • Pass the start, stop and step, index values as arguments to the slice() method.
  • Store it in another variable.
  • Pass the above slice values to the array module array to slice it and print it.
  • Pass the above slice values to the numpy module array to slice it and print it.
  • The Exit of the Program.

Below is the implementation:

# Import array module using the import Keyword
import array
# Import numpy module as np using the import Keyword
import numpy as np
# Call the array function from array module and initialize with some random values
# Store it in a variable.
arraymodle_arry = array.array('i', [10, 11, 12, 13, 14, 15])
# Call the array function from numpy module and initialize with some random values
# Store it in another variable.
numpy_arry = np.array([21, 22, 12, 23, 25, 9])
# Pass the start, stop and and step index values as arguments to the slice() method.
# Store it in another variable.
slice_vals = slice(2, 6, 2)
# Pass the above slice values to the array module array to slice it and print it
print("The sliced Array of the array module = ", arraymodle_arry[slice_vals])
# Pass the above slice values to the numpy module array to slice it and print it
print("The sliced Array of the numpy module =  ", numpy_arry[slice_vals])

Output:

The sliced Array of the array module = array('i', [12, 14]) 
The sliced Array of the numpy module = [12 25]

Leave a Comment