Python slice 2d array – How to Slice Multidimensional Array in Python?

Python slice 2d array: We’ve all heard about arrays, which are the simplest way to deal with a big number of the same data type. Arrays are much easier to work with and give us with a variety of features. For example, if you need to store a large quantity of data, you will almost certainly use an array rather than manually building a list. A multi-dimensional array is one that has many rows and columns.

What is slicing?

How to slice an array in python: In Python, slicing implies taking elements from one index to another i.e a part of the string, list, etc.

We use slice instead of index, as seen below: [start:end].

We can alternatively specify the step as [start:end:step].

The start index is where you want to start, the end index is where you want to stop, and the jump index is where you want to skip certain indices between the start and stop.

If we don’t pass start, it’s assumed to be 0.

If we do not pass the end, the length of the array in that dimension is assumed.

If we do not pass the step, it is assumed to be 1.

Let us now see the slicing of a multi-Dimensional Array.

Slice Multidimensional Array in Python

1) Creating a multi-Dimensional Array:

Approach:

  • Import numpy module using the import keyword.
  • Pass a list of lists(multi-dimensional array) to the array() function of the numpy module to create a multidimensional array.
  • Print the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword.
import numpy as np
# Pass list of lists(multi-dimensional array) to the array() function
# of the numpy module to create a multi-dimensional array
gvn_arry =np.array([[10, 20, 30, 40, 50],[15, 25, 35, 45, 55]])
# Print the given array
print("The given array is:")
print(gvn_arry)

Output:

The given array is:
[[10 20 30 40 50]
 [15 25 35 45 55]]

2)Slicing the Array to get the first row:

# Printing the first row of the given array
print(gvn_arry[0][:])

Output:

[10 20 30 40 50]

3)Slicing the Array to get the second row:

# Printing the second row of the given array
print(gvn_arry[1][:])

Output:

[15 25 35 45 55]

4)Slicing the Array to get Alternate elements:

# Printing alternate elements of the given array
print(gvn_arry[::, ::2])

Output:

[[10 30 50]
 [15 35 55]]

5) Removing the start, end elements:

# Removing the start, end elements of the given multidimensional array
print(gvn_arry[:, 1:-1])

Output:

[[20 30 40]
 [25 35 45]]

6) Printing the elements of second-row index 1 to index 5(not included):

# Printing the elements of second row index 1 to index 5(not included):
print(gvn_arry[1, 1:5])

Output:

[25 35 45 55]

7) Creating a 3-Dimensional Array:

# Import numpy module using the import keyword.
import numpy as np
# Pass list of lists(multi-dimensional array) to the array() function
# of the numpy module to create a multi-dimensional array
# Here we create a 3D array.
gvn_arry =np.array([[10, 20, 30],[15, 25, 35], [50, 60, 70]])
# Print the given 3D array
print("The given 3D array is:")
print(gvn_arry)

Output:

The given 3D array is:
[[10 20 30]
 [15 25 35]
 [50 60 70]]

Entire Code:

# Import numpy module using the import keyword.
import numpy as np
# Pass list of lists(multi-dimensional array) to the array() function
# of the numpy module to create a multi-dimensional array
gvn_arry =np.array([[10, 20, 30, 40, 50],[15, 25, 35, 45, 55]])
# Print the given array
print("The given array is:")
print(gvn_arry)
print()
# Printing the first row of the given array
print(gvn_arry[0][:])
print()
# Printing the second row of the given array
print(gvn_arry[1][:])
print()
# Printing alternate elements of the given array
print(gvn_arry[::, ::2])
print()
# Removing the start, end elements of the given multidimensional array
print(gvn_arry[:, 1:-1])
print()
# Printing the elements of second row index 1 to index 5(not included):
print(gvn_arry[1, 1:5])

Output:

The given array is:
[[10 20 30 40 50]
[15 25 35 45 55]]

[10 20 30 40 50]

[15 25 35 45 55]

[[10 30 50]
[15 35 55]]

[[20 30 40]
[25 35 45]]

[25 35 45 55]