Built with Tutify.io

    AIStudyLabs

    Check Given Object is a List or not in Python

    Check Given Object is a List or not in Python

    How to check if a given object is a list or not in Python? In python, there are several ways to check if the given object is a list or not. When you use these methods, you need to specify what type you are checking with and if the object is that specific type it returns True or else Flase. In our case, we need to specify the list as an argument.

    The following are methods to check if an object is a list or not

    1. Using isinstance()
    2. Using isinstance() with typing.List
    3. Using type()
    4. Using class

    <a id="h-1-quick-examples-to-check-whether-object-is-list-or-not"></a>

    1. Quick Examples to Check Whether Object is List or Not

    Following are quick examples to check if the object is a List or not in python.

    
    # Below are some quick examples.
    
    # Using isinstance()
    if (isinstance(countries, list)):
      print("List")
    else:
        print("not a List")
    
    # Using isinstance() with typing.List
    if (isinstance(countries, typing.List)):
      print("List")
    else:
        print("not a List")
    
    # Using type()
    if (type(countries) is list):
      print("List")
    else:
        print("not a List")
    
    # Using __class__
    if (countries.__class__ == list):
      print("List")
    else:
        print("not a List")
    

    <a id="h-2-using-isinstance-to-check-if-object-is-list-in-python"></a>

    2. Using isinstance() to Check If Object is List in Python

    The isinstance() method in python is used to check if the given object is a list or not. Actually, this method is used to check if object of a specific type, not just lists. This method takes two parameters first, the object, and second the type you wanted to check with. If the input object is of a specified type, it returns True, otherwise False is returned.

    Here, we will pass our object as the first parameter and list as the second parameter. Let's see how to use isinstance() method.

    
    # Syntax
    isinstance(object, list)
    

    Let's create two objects, one is a string and another is a list of countries. We will check if both are list objects or not using isinstance() method.

    
    countries=['Russia', 'Japan', 'Mexico', 'Europe']
    city='Guntur'
    
    # Using isinstance() to check countries is list
    if (isinstance(countries, list)):
      print("List")
    else:
        print("not a List")
    
    # Using isinstance() to check city is list
    if (isinstance(city, list)):
      print("List")
    else:
        print("not a List")
    

    Yields below output.

    We can see that countries is a List type and city is not a list.

    <a id="h-3-using-isinstance-with-typing-list"></a>

    3. Using isinstance() with typing.List

    It is similar to the above method. But, we will pass typing List as a second parameter to the isinstance() method instead of list. Here, typing is a module that we need to import.

    Let's see how to use isinstance() method with typing.List.

    
    # Syntax
    isinstance(object, typing.List)
    

    Let's use two objects. One is a string and another is a list of countries. We will check if both are list objects or not using isinstance() method.

    
    import typing
    # Using isinstance() with typing.list to check countries is list
    if (isinstance(countries, typing.List)):
      print("List")
    else:
        print("not a List")
    
    # Using isinstance() with typing.list to check city is list
    if (isinstance(city, typing.List)):
      print("List")
    else:
        print("not a List")
    

    Yields the same output as above.

    <a id="h-4-check-if-given-object-is-list-using-type"></a>

    4. Check If Given Object is List Using type()

    type() will return the type of the object. Here, we will compare the object returning type with the list using is operator. If the object is a list, True is returned, Otherwise False is returned. Let's see the syntax of type() with is operator.

    
    # Syntax
    type(object) is list
    

    Let's use it with an example.

    
    # Using type() to check countries is list
    if (type(countries) is list):
      print("List")
    else:
        print("not a List")
    
    # Using type() to check city is list
    if (type(city) is list):
      print("List")
    else:
        print("not a List")
    
    # Output:
    # List
    # not a List
    

    <a id="h-5-using-class"></a>

    5. Using class

    In Python __class__ will return the class of the given object. Here, we will compare the object with list using == operator. If the object is list, True is returned, Otherwise False is returned. Let's see an example

    
    # Using __class__ to check countries is list
    if (countries.__class__ == list):
      print("List")
    else:
        print("not a List")
    
    # Using _class_ to check city is list
    if (city.__class__ == list):
      print("List")
    else:
        print("not a List")
    
    # Output:
    # List
    # not a List
    

    <a id="h-6-conclusion"></a>

    6. Conclusion

    We have seen several ways to check if the object is a list or not in python using isinstance(), and isinstance() with typing.List, type() and __class__. In all these methods, we used a if statement that will print a statement specifying whether the object is a list or not.

    With most of these methods, you need to specify what type you are checking with and if the object is that specific type it returns True or else Flase. In our case, we need to specify the list as an argument.

    <a id="h-related-articles"></a>

    Related Articles