Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How to check if a variable is a list in Python?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJul 14, 2023
UpdatedNov 17, 2026
5 min read

To check if a variable is a list in Python, use isinstance(value, list). It returns True for a built-in list and for any subclass of it, which is almost always what you want. Avoid type(value) == list unless you must exclude subclasses on purpose.

The checks below work on Python 3.12 and later, and on older 3.x releases too. The article also covers type hints, structural checks with collections.abc, and the mistakes that make a type check return the wrong answer.

Table of Contents

  • Using isinstance() to check for a list
  • Why type() is stricter than isinstance()
  • Checking against an abstract type with collections.abc
  • Common mistakes when checking for a list
  • Using the check in functions and type hints
  • Frequently Asked Questions
    • What is the difference between isinstance() and type() for a list?
    • Does isinstance(value, list) work for list subclasses and collections.UserList?
    • Can I use isinstance() with a parameterised type such as list[int]?
    • Should I check types at all in Python?
  • Summary

Using isinstance() to check for a list

isinstance(obj, cls) returns True when obj is an instance of cls or of a class that inherits from it. It is the standard type check in Python and the one the language documentation recommends.

python

You can pass a tuple of types to test several at once, for example isinstance(value, (list, type(None))).

Why type() is stricter than isinstance()

type(value) is list compares the exact class. A subclass instance fails that test, even though it behaves like a list everywhere else.

python

Prefer is over == when you do compare types, because classes are singletons. Use the exact-type check only when a subclass would genuinely break your logic, for example when you serialise values and must reject custom subclasses.

Checking against an abstract type with collections.abc

list is one of several sequence types. If your function also accepts tuples or other ordered collections, test against collections.abc.Sequence instead. Remember that str and bytes are sequences too, so exclude them explicitly when you mean a collection of items.

python
text

Use the built-in type when you need list-specific behaviour, and the abstract type when you only need the shared interface. That keeps functions flexible for callers.

Common mistakes when checking for a list

The pattern below shows up in older tutorials, but it does not test the type at all.

python

The try/except adds nothing here: isinstance() never raises for ordinary values, so the len() call only makes the function fail on objects without a length. Use try/except to handle real failures, not to detect a type.

A related trap is treating a string as a list because both can be iterated. isinstance("abc", Sequence) is True, while isinstance("abc", list) is False, so choose the check that matches your intent.

Using the check in functions and type hints

Validate data at the boundary, where it enters your program, and raise a clear error when it is wrong.

python
text

Raise TypeError for a wrong type and ValueError for a right type with a wrong value. Type hints such as data: list document the contract and let tools like mypy or pyright catch mistakes before the code runs, but they are not enforced at runtime, so keep the isinstance() check where the input is untrusted.

Frequently Asked Questions

What is the difference between isinstance() and type() for a list?

isinstance() accepts subclasses and supports a tuple of types. type() returns the exact class, so a subclass instance is not equal to list.

Does isinstance(value, list) work for list subclasses and collections.UserList?

It works for real subclasses of list. collections.UserList is not a list subclass, so use collections.abc.MutableSequence if you want to accept it.

Can I use isinstance() with a parameterised type such as list[int]?

No. isinstance(value, list[int]) raises TypeError, because generic aliases exist for static type checkers only. Check isinstance(value, list) first, then inspect the elements yourself if you need to.

Should I check types at all in Python?

Python favours duck typing, so check types only where wrong input would cause bad behaviour, such as at API boundaries. Inside your own code, rely on type hints and tests.

Summary

Use isinstance(value, list) to check whether a variable is a list in Python. Reach for type(value) is list only when subclasses must be rejected, and use an abstract class from collections.abc when any compatible object will do. Avoid try/except tricks as type tests, and raise TypeError when input has the wrong type.

Share