Open In App

Slice a 2D List in Python

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Slicing a 2D list in Python is a common task when working with matrices, tables, or any other structured data. It allows you to extract specific portions of the list, making it easier to manipulate and analyze the data. In this article, we’ll explore four simple and commonly used methods to slice a 2D list in Python.

How To Slice A 2D List In Python?

Below, are the methods of How To Slice A 2D List In Python.

Create 2D List

First we created the 2D list name matrix , and print the 2D list.

Python3




# Sample 2D list
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
print(matrix)


Output

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Slice A 2D List In Python Using Basic Slicing

The first code extracts rows from index 1 to 2 (exclusive) of a 2D list named ‘matrix’ and assigns the result to the variable ‘rows_slice’. The second code creates a new list ‘columns_slice’ by slicing columns from index 0 to 2 (exclusive) in all rows of the ‘matrix’.

Python3




# Slicing rows from index 1 to 2 (exclusive)
rows_slice = matrix[1:2]
print("Sliced Rows:", rows_slice)
 
# Slicing columns from index 0 to 2 (exclusive) in all rows
columns_slice = [row[0:2] for row in matrix]
print("Sliced Columns:", columns_slice)


Output

Sliced Rows: [[4, 5, 6]]
Sliced Columns: [[1, 2], [4, 5], [7, 8]]

Slice A 2D List In Python Using Condition Statement

In this example, below code extracts rows from a matrix where at least one element is greater than 5 and prints them. Similarly, it extracts columns from the matrix whose sum is greater than 10 and prints them.

Python3




# Slicing rows based on a condition (e.g., elements greater than 5)
rows_condition = [row for row in matrix if any(element > 5 for element in row)]
print("Rows with Elements > 5:", rows_condition)
 
# Slicing columns based on a condition (e.g., columns with sum greater than 10)
columns_condition = [col for col in zip(*matrix) if sum(col) > 10]
print("Columns with Sum > 10:", columns_condition)


Output

Rows with Elements > 5: [[4, 5, 6], [7, 8, 9]]
Columns with Sum > 10: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Slice A 2D List In Python Using Itertools Function

In this example below code uses `islice` from the `itertools` module to slice rows and columns from a matrix. It extracts a specific range of rows (1 to 2) and a specific range of elements (0 to 2) from each row and prints them as sliced rows and columns, respectively.

Python3




from itertools import islice
 
# Slicing rows using itertools.islice
rows_islice = list(islice(matrix, 1, 2))
print("islice Sliced Rows:", rows_islice)
 
# Slicing columns using itertools.islice
columns_islice = [list(islice(row, 0, 2)) for row in matrix]
print("islice Sliced Columns:", columns_islice)


Output

islice Sliced Rows: [[4, 5, 6]]
islice Sliced Columns: [[1, 2], [4, 5], [7, 8]]

Slice A 2D List In Python Using NumPy Library

In this example, below code converts a list named ‘matrix’ into a NumPy array (‘matrix_np’) and then uses NumPy slicing to extract a specific range of rows (1 to 2) and a specific range of columns (0 to 2). The sliced rows and columns are printed as ‘NumPy Sliced Rows’ and ‘NumPy Sliced Columns’, respectively.

Python3




import numpy as np
 
# Convert the list to a NumPy array
matrix_np = np.array(matrix)
 
# Slicing rows using NumPy
rows_slice_np = matrix_np[1:2, :]
print("NumPy Sliced Rows:", rows_slice_np)
 
# Slicing columns using NumPy
columns_slice_np = matrix_np[:, 0:2]
print("NumPy Sliced Columns:", columns_slice_np)


Output

NumPy Sliced Rows: [[4 5 6]]
NumPy Sliced Columns: [[1 2]
[4 5]
[7 8]]

Conclusion

In conclusion, slicing a 2D list in Python allows for efficient extraction of specific elements or sublists based on specified ranges. The syntax for slicing involves using square brackets and colons to denote start, stop, and step parameters. This powerful feature enhances code readability and simplifies data manipulation tasks, providing a convenient way to access and manipulate elements within a two-dimensional structure.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads