How to Retrieve an Entire Row or Column of an Array in Python?


Python provides various in-built methods to retrieve an entire row or column of an array. We can use the Slice notation method, Numpy Library, list comprehension, and for loop to retrieve the entire row or column of an array. In this article, we will explore all the methods with examples to retrieve the row or column of an array.

Method 1: Using Slice Notation

Using slice notation we extract a subset of elements from an array. We use the “:” notation to specify the start and end index of the subset. To retrieve an entire row or column we need to specify either the start or end index as “:”.

Syntax

array_name[start_index:end_index]

Here, array_name is the name of the array and start_index is the start index of the element which you want to extract and end_index is the index of the last element which you want to exclude from the list.

Example

In the below example, we have created a 2D array using the NumPy library. We then retrieved the second row and second column of the array using slice notation. We have specified ":" for the start index to get the entire row, and ":" for the end index to get the entire column.

# Retrieving an entire row of an array using slice notation
import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Retrieve the second row
row = arr[1, :]

print("Entire Row:")
print(row)

# Retrieve the second column
col = arr[:, 1]

print("Entire Column:")
print(col)

Output

Entire Row:
[4 5 6]
Entire Column:
[2 5 8]

Method 2: Using Numpy Array

Numpy is a Python library mostly used for working with arrays. It provides many functions for creating, manipulating, and performing operations on arrays. Numpy provides the “take” function to retrieve an entire row or column if an array in Python.

Syntax

numpy.take(array, indices, axis= None)

Here, the take function takes three arguments - the array from which we want to retrieve the row or column, the row or column indices, and the axis along which we want to retrieve the row or column. If we want to retrieve the row we specify the axis=0 and if want to retrieve the column we specify the axis=1.

Example

In the below example, we have used the NumPy "take" function to retrieve the second row of the array. We have specified the indices=[1] to retrieve the second row, and axis=0 to indicate that we want to retrieve a row. Similarly, we can use the NumPy "take" function to retrieve an entire column of an array.

# Retrieving an entire row of an array using NumPy
import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Retrieve the second row using NumPy
row = np.take(arr, indices=[1], axis=0)

print("Entire Row:")
print(row)

# Retrieve the second column using NumPy
col = np.take(arr, indices=[1], axis=1)

print("Entire Column:")
print(col)

Output

Entire Row:
[[4 5 6]]
Entire Column:
[[2]
 [5]
 [8]]

Method 3: Using List Comprehension

We can retrieve an entire row or column of an array using list comprehension by looping through the rows and columns of the array and appending the element to a list. The list comprehension is a more concise way of creating a list in Python.

Syntax

newList = [expression(element) for element in oldList if condition]

Here, expression is the looping over the elements in the old list and checking if the condition is true or false for every element in the list. If the condition is true then the element is added to the new list.

Example

In the below example, we have used list comprehension to retrieve the second row and second column of the array. We have looped through the rows and columns of the array and appended the elements to a list.

# Retrieving an entire row of an array using list comprehension
import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Retrieve the second row
row = [arr[1, j] for j in range(arr.shape[1])]

print("Entire Row:")
print(row)

# Retrieve the second column
col = [arr[i, 1] for i in range(arr.shape[0])]

print("Entire Column:")
print(col)

Output

Entire Row:
[4, 5, 6]
Entire Column:
[2, 5, 8]

Method 4: Using for Loop

To retrieve an entire row or column of an array we can simply loop over the rows and columns of the array and store the element in the list.

Syntax

For i in range (array)

Here, i is the index that is used to iterate over the row and column of an array.

Example

In the below example, we have used a for loop to retrieve the second row and second column of the array. We have looped through the rows and columns of the array and stored the elements in a list.

# Retrieving an entire row of an array using for loop
import numpy as np

# Create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Retrieve the second row
row = []
for j in range(arr.shape[1]):
   row.append(arr[1, j])

print("Entire Row:")
print(row)

# Retrieve the second column
col = []
for i in range(arr.shape[0]):
   col.append(arr[i, 1])

print("Entire Column:")
print(col)

Output

Entire Row:
[4, 5, 6]
Entire Column:
[2, 5, 8]

Conclusion

In this article we have discussed how we can retrieve an entire row or column of an array using methods like - slice notation, using numpy array, list comprehension, and a for loop. Slice notation uses the “:” notation to get the entire row or column of an array. List comprehension provides a concise way of creating the list in Python. Depending on the size of the array we can choose any of the methods to retrieve an entire row or column of an array.

Updated on: 11-Jul-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements