Python | Check if list is Matix

Sometimes, while working with Python lists, we can have a problem in which we need to check for a Matrix. This type of problem can have a possible application in Data Science domain due to extensive usage of Matrix. Let’s discuss a technique and shorthand which can be used to perform this task.

Method : Using isinstance() + all()
This problem can be performed using the combination of above functions. The all() can be used to check for all the elements of list and isinstance function checks for the list datatype in list. The logic behind is, every element of list must be a list to qualify it as matrix.





# Python3 code to demonstrate working of
# Check if list is Matix
# using isinstance() + all()
# initialize lists
test_list = [[4, 5], [5, 8], [9, 10]]
# printing original list
print("The original list is : " + str(test_list))
# Check if list is Matix
# using isinstance() + all()
res = all(isinstance(ele, list) for ele in test_list)
# printing result
print("Is list a Matrix ?: " + str(res))


Output :

The original list is : [[4, 5], [5, 8], [9, 10]]
Is list a Matrix ?: 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 or mail your article to 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 to report any issue with the above content.