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 particular column of Matrix. Let’s discuss a shorthand by which this task can be performed.

Method : Using any() + list comprehension
This task can be performed using combination of above functions in which we imply list comprehension for iteration and support tasks and any() can be used to check for any occurrence of the desired character.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Search in Nth Column of Matrix
# Using any() + list comprehension
  
# initializing list 
test_list = [[1, 4, 5], [6, 7, 8], [8, 3, 0]]
  
# printing list 
print("The original list : " + str(test_list))
  
# initializing N 
N = 1 
  
# initializing num
ele = 3
  
# Search in Nth Column of Matrix
# Using any() + list comprehension
res = any(sub[N] == ele for sub in test_list)
  
# Printing result
print("Does element exists in particular column : " + str(res))

chevron_right


Output :

 
The original list : [[1, 4, 5], [6, 7, 8], [8, 3, 0]]
Does element exists in particular column : True


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.