![]() |
VOOZH | about |
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:
isinstance() function checks if an object is an instance of a specific class or data type.
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.
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.
Object is a list Object is not a list
Explanation:
To check if an object is a list , we can use the __class__ attribute and compare it to the list type:
input is a list input is not a list
Explanation: