Related Articles

# Python | Check if list is Matrix

• Last Updated : 08 Jul, 2021

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

 `# Python3 code to demonstrate working of``# Check if list is Matrix``# 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 Matrix``# 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```

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up