Search Elements in a Matrix - Python
Last Updated :
03 Feb, 2025
The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1, 11, 18]], the task would be to check if an element like 13 is present. The result would be True if found, or False if the element does not exist within the matrix.
Using list comprehension
any() is a built-in Python function that checks if any element in an iterable evaluates to True. When combined with list comprehension, it allows us to check if an element exists in any of the sublists in a nested list or matrix .
Python
a = [[4, 5, 6], [10, 2, 13], [1, 11, 18]]
res = any(13 in sub for sub in a)
print(str(res))
Explanation: any(13 in sub for sub in a) checks if 13 is present in any row of the matrix a. It uses a generator expression with any(), which returns True as soon as 13 is found.
Using numpy
NumPy is a powerful library for numerical computations . It provides a highly efficient way to work with large matrices due to vectorized operations. This eliminates the need for explicit loops, making operations faster and easier to write.
Python
import numpy as np
# Create a 2D NumPy array
a = np.array([[4, 5, 6], [10, 2, 13], [1, 11, 18]])
res = np.any(a == 13)
print(res)
Explanation: np.any(a == 13) checks if 13 exists in the NumPy array a. The expression (a == 13) creates a boolean array and np.any() returns True if at least one True value is found, ensuring an efficient search.
itertools.chain function from itertools module that takes multiple iterables and combines them into a single iterable. This is useful when we want to flatten a matrix and perform operations on all elements without explicitly nesting loops.
Python
import itertools
a = [[4, 5, 6], [10, 2, 13], [1, 11, 18]]
res = 19 in itertools.chain(*a)
print(str(res))
Explanation: 19 in itertools.chain(*a) flattens the 2D list a and checks if 19 is present in the flattened sequence, offering a efficient way to search for the element.
Using for loop
This is the most explicit method, where we manually iterate through the matrix using a nested for loop. It checks each row to see if the target element is present and exits early once a match is found.
Python
a = [[4, 5, 6], [10, 2, 13], [1, 11, 18]]
found = False # Initialize a variable 'found'
for row in a:
if 19 in row:
found = True
break
print(str(found))
Explanation: for loop iterates through each row of the matrix a. If 19 is found in a row, found is set to True and the loop exits early with break.
Similar Reads
Python - Check Similar elements in Matrix rows Given a Matrix and list, the task is to write a Python program to check if all the matrix elements of the row are similar to the ith index of the List. Input : test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Output : True Explanation : All rows have same elements.Input : test_list = [[1, 1,
8 min read
Python | Search in Nth Column of Matrix Sometimes, while working with Python Matrix, we can have a problem in which we need to check if an element is present or not. This problem is simpler, but a variation to it can be to perform a check in a particular column of Matrix. Let's discuss a shorthand by which this task can be performed. Met
10 min read
Python | Row with Minimum element in Matrix We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
5 min read
Linear Search - Python Given an array, arr of n elements, and an element x, find whether element x is present in the array. Return the index of the first occurrence of x in the array, or -1 if it doesnât exist.Examples:Input: arr[] = [10, 50, 30, 70, 80, 20, 90, 40], x = 30Output : 2Explanation: For array [10, 50, 30, 70,
4 min read
Find element in Array - Python Finding an item in an array in Python can be done using several different methods depending on the situation. Here are a few of the most common ways to find an item in a Python array.Using the in Operatorin operator is one of the most straightforward ways to check if an item exists in an array. It r
3 min read