Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Program to check Identity Matrix
Next article icon

Python | Check if list is Matrix

Last Updated : 06 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

 

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(1) constant additional space is needed

Method #2: Using filter()+isinstance() + lambda functions

Python3




# Python3 code to demonstrate working of
# Check if list is Matrix
# 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 = list(filter(lambda x: isinstance(x, list), test_list))
if(len(res) == len(test_list)):
    res = True
else:
    res = False
# 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

Time Complexity:O(N)
Auxiliary Space: O(N)

Method3: Using the type()+all() function: 

Use the type() function to check if all elements of the list are of type list. Time complexity of this approach would be O(n) and space complexity would be O(1)

Python3




# initialize list
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 type() function
res = all(type(i) is list for i in test_list)
 
# printing result
print("Is list a Matrix ?: " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
 
 
Output
The original list is : [[4, 5], [5, 8], [9, 10]] Is list a Matrix ?: True

This approach uses the type() function to check if all elements of the list are of type list. 
Time complexity: O(n) 
Auxiliary space: O(1).
 



Next Article
Program to check Identity Matrix
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Check if a List is Sorted or not - Python
    We are given a list of numbers and our task is to check whether the list is sorted in increasing or decreasing order. For example, if the input is [1, 2, 3, 4], the output should be True, but for [3, 1, 2], it should be False. Using all()all() function checks if every pair of consecutive elements in
    3 min read
  • Check if a list is empty or not in Python
    In article we will explore the different ways to check if a list is empty with simple examples. The simplest way to check if a list is empty is by using Python's not operator. Using not operatorThe not operator is the simplest way to see if a list is empty. It returns True if the list is empty and F
    2 min read
  • Python - Check if one list is subset of other
    Checking if one list is a subset of another is a common task when working with data collection. In this article, we will see different ways to perform this check. Using set.issubset() Method issubset() method is the best way to check if all elements of one list exist in another. It Converts lists to
    2 min read
  • Program to check Identity Matrix
    Given a square matrix mat[][] of order n*n, the task is to check if it is an Identity Matrix. Identity Matrix: A square matrix is said to be an identity matrix if the elements of main diagonal are one and all other elements are zero. The identity Matrix is also known as the Unit Matrix. Examples: In
    5 min read
  • Check If Value Is Int or Float in Python
    In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to check if a value is an integer
    4 min read
  • How to Check If Python Package Is Installed
    In this article, we are going to see how to check if Python package is installed or not. Check If Python Package is InstalledThere are various methods to check if Python is installed or not, here we are discussing some generally used methods for Check If Python Package Is Installed or not which are
    5 min read
  • Multi-dimensional lists in Python
    There can be more than one additional dimension to lists in Python. Keeping in mind that a list can hold other lists, that basic principle can be applied over and over. Multi-dimensional lists are the lists within lists. Usually, a dictionary will be the better choice rather than a multi-dimensional
    3 min read
  • Check if two given matrices are identical - Python
    In Python, we often need to check whether two given matrices are identical. Two matrices are considered identical if they have the same number of rows and columns and the corresponding elements in each matrix are equal. Let's explore various methods to compare two matrices and check if they are iden
    3 min read
  • Pymatrix module in python
    Pymatrix is a lightweight matrix library which supports basic linear algebra operations. The elements in matrix should be numeric type to support basic algebra operations - int, float, rational, complex.  Instantiating Matrix   Using Matrix constructor Matrix can be initialised using constructor of
    3 min read
  • Check if element exists in list in Python
    In this article, we will explore various methods to check if element exists in list in Python. The simplest way to check for the presence of an element in a list is using the in Keyword. Example: [GFGTABS] Python a = [10, 20, 30, 40, 50] # Check if 30 exists in the list if 30 in a: print("Eleme
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences