Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Check If a Given Object is a List in Python

  • 24 Nov, 2025
  • Com 0
How to Check If an Object is a List in Python

Method 1: Using isinstance()

The standard and recommended way to check whether an object is a list in Python is to use the isinstance(obj, list) function. It accepts an input object as the first argument and a list as the second argument to verify that the object is a list. If it is a list, returns True; otherwise, returns False.

How to Check if an object is a list in Python

def is_list(obj):
    return isinstance(obj, list)


print(is_list([]))
# Output: True

print(is_list([1, 2, 3]))
# Output: True

print(is_list(tuple()))
# Output: False

print(is_list("hello"))
# Output: False

If an input is an empty list or a list with elements, it returns True.

Custom subclasses of list

The main advantage of the isinstance(obj, list) method is that it handles inheritance and returns True for custom subclasses of list. The key reason to use this approach.

class MyList(list):
    def say_hi(self):
        print("Hello, I'm a fancy list!")


class QuerySet(list):
    def filter(self, *args): ...


class SortedList(list):
    def append(self, item):
        super().append(item)
        self.sort()


print(isinstance(MyList(), list))
# Output: True

print(isinstance(QuerySet(), list))
# Output: True

print(isinstance(SortedList([3, 1, 2]), list))
# Output: True

In this code, we created three types of subclasses of the list. Since all are lists, the isinstance() method returns True, and it supports all the basic list functions like extend(), append(), or remove().

Method 2: Using type(obj) is list: Strict Type Match

If you want to enforce strict typing on an input variable, you can use the type() method.

list_obj = [1, 2, 3]

if type(list_obj) is list:
    print("Exact list only, no subclasses")

# Output: Exact list only, no subclasses

Subclass of a list

The type() method does not accept subclasses and only accepts real lists. With the help of an if condition and is operator, it returns False for a subclass of a list.

class MyList(list):
    def say_hi(self):
        print("Hello, I'm a fancy list!")


class QuerySet(list):
    def filter(self, *args): ...


class SortedList(list):
    def append(self, item):
        super().append(item)
        self.sort()


print(type(MyList()) is list)
# Output: False

print(type(QuerySet()) is list)
# Output: False

print(type(SortedList()) is list)
# Output: False

In this code, we defined three classes that are subclasses of list, and for each subclass, type() returns False when passed a list.

It prevents accidental acceptance of custom list-like types, which can be an advantage in some cases.

That’s all!

Post Views: 3
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Repeat an Element in Python List
How to Concatenate or Join Two Lists in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend