Python | Check If A Given Object Is A List Or Not (original) (raw)
Last Updated : 16 Apr, 2025
Given an object, the task is to check whether the object is a list or not. Python provides few simple methods to do this and in this article, we’ll walk through the different ways to check if an object is a list:
Using isinstance()
**isinstance() function checks if an object is an instance of a specific class or data type.
Python `
if isinstance([1, 2, 3], list): print("Object is a list") else: print("Object is not a list")
if isinstance("12345", list): print("Object is a list") else: print("Object is not a list")
`
Output
Object is a list Object is not a list
**Explanation: isinstance(obj, list) returns **True if obj is a and False if it’s not.
Using type()
Another method to check if an object is a list is by using the type() function. This function returns the exact type of an object.
Python `
l1 = [1, 2, 3] l2 = (10, 20, 30) # A tuple
if type(l1) is list: print("Object is a list") else: print("Object is not a list")
if type(l2) is list: print("Object is a list") else: print("Object is not a list")
`
Output
Object is a list Object is not a list
**Explanation:
- **type(obj) returns the exact type (e.g., list, tuple, dict).
- Unlike **isinstance(), it does not consider inheritance, useful when strict type matching is needed.
Using __class__ Attribute
To check if an object is a list , we can use the __class__ attribute and compare it to the list type:
Python `
l1 = [1, 2, 3, 4, 5] l2 = (12, 22, 33)
if l1.class == list: print("input is a list") else: print("input is not a list")
if l2.class == list: print("input is a list") else: print("input is not a list")
`
Output
input is a list input is not a list
**Explanation:
- **__class__ returns the class of an object.
- While it works, it’s less readable and not as commonly used as **isinstance().
Similar Reads
- Python | Check if a list exists in given list of lists Given a list of lists, the task is to check if a list exists in given list of lists. Input : lst = [[1, 1, 1, 2], [2, 3, 4], [1, 2, 3], [4, 5, 6]] list_search = [4, 5, 6] Output: True Input : lst = [[5, 6, 7], [12, 54, 9], [1, 2, 3]] list_search = [4, 12, 54] Output: False Let’s discuss certain ways 4 min read
- Check If Python Json Object is Empty Python users frequently work with JSON, particularly when exchanging data between multiple devices. In this article, we'll learn how to check if a JSON object is empty using Python. Check If a JSON Object Is Empty in PythonBelow, we provide examples to illustrate how to check if a JSON object is emp 3 min read
- Check If Value Exists in Python List of Objects When working with Python, we might need to check whether a specific value exists in a list of objects. This problem often arises when dealing with data stored as objects and we want to verify the presence of a specific property value. Using any() Function with List Comprehensionany() function is an 4 min read
- Python Program to Check if a given matrix is sparse or not A matrix is a two-dimensional data object having m rows and n columns, therefore a total of m*n values. If most of the values of a matrix are 0 then we say that the matrix is sparse. Consider a definition of Sparse where a matrix is considered sparse if the number of 0s is more than half of the elem 4 min read
- Python program to check if a given string is Keyword or not This article will explore how to check if a given string is a reserved keyword in Python. Using the keyword We can easily determine whether a string conflicts with Python's built-in syntax rules. Using iskeyword()The keyword module in Python provides a function iskeyword() to check if a string is a 2 min read
- Check if a Nested List is a Subset of Another Nested List - Python The task is to check if all sublists in one nested list are present in another nested list. This is done by verifying whether each sublist in the second list exists in the first list. For example, given list1 = [[2, 3, 1], [4, 5], [6, 8]] and list2 = [[4, 5], [6, 8]], we check if both [4, 5] and [6, 4 min read
- Python | Check if front digit is Odd in list Sometimes we may face a problem in which we need to find a list if it contains numbers that are odd. This particular utility has an application in day-day programming. Let’s discuss certain ways in which this task can be achieved. Method #1 : Using list comprehension + map() We can approach this pro 7 min read
- Check if a Number is a Whole Number in Python Floating-point numbers in Python can sometimes pose challenges when you need to determine whether they represent whole numbers. Due to the inherent precision limitations of floating-point representation, comparing them directly for equality with integers may lead to unexpected results. In this artic 3 min read
- Python | Check if a list is contained in another list Given two lists A and B, write a Python program to Check if list A is contained in list B without breaking A's order. Examples: Input : A = [1, 2], B = [1, 2, 3, 1, 1, 2, 2] Output : TrueInput : A = ['x', 'y', 'z'], B = ['x', 'a', 'y', 'x', 'b', 'z'] Output : FalseApproach #1: Naive Approach A simpl 6 min read
- Check if String is Empty or Not - Python We are given a string and our task is to check whether it is empty or not. For example, if the input is "", it should return True (indicating it's empty), and if the input is "hello", it should return False. Let's explore different methods of doing it with example: Using Comparison Operator(==)The s 2 min read