Python
type-checking
list
programming
duplicate

Checking if type list in python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Checking whether a value is a list in Python is simple, but the right tool depends on how strict you want the check to be. Sometimes you want exactly the built-in list type. In other cases, you want to accept subclasses or even any sequence-like object.

That distinction matters because Python's type system is flexible, and the most obvious check is not always the most Pythonic one.

Exact Type Check Versus isinstance

If you want to know whether a value is exactly a built-in list, type(value) is list gives a strict answer:

python
value = [1, 2, 3]
print(type(value) is list)

That is precise, but it does not accept subclasses. If a custom class inherits from list, the expression returns False.

In most application code, isinstance is the better choice:

python
value = [1, 2, 3]
print(isinstance(value, list))

isinstance supports inheritance and is generally the recommended way to ask whether an object behaves as an instance of a type hierarchy.

Why isinstance Is Usually Better

Consider a subclass:

python
1class MyList(list):
2    pass
3
4value = MyList([1, 2, 3])
5
6print(type(value) is list)
7print(isinstance(value, list))

The first result is False, while the second is True. That is often what you want, because the subclass is still functionally a list.

Accepting More Than Just Lists

Sometimes checking specifically for list is too narrow. If your function only needs an ordered iterable, a broader protocol may be more appropriate. For example, tuples and some custom sequence types may work perfectly well.

You can check against abstract base classes when that better reflects intent:

python
1from collections.abc import Sequence
2
3print(isinstance([1, 2, 3], Sequence))
4print(isinstance((1, 2, 3), Sequence))
5print(isinstance("abc", Sequence))

This is useful when the requirement is "sequence-like" rather than "must be a mutable list." Be careful, though: strings are also sequences, and that is sometimes not what you want.

Writing Clear Validation Logic

In many functions, explicit validation is enough:

python
1def process_items(items):
2    if not isinstance(items, list):
3        raise TypeError("items must be a list")
4    return [item * 2 for item in items]
5
6
7print(process_items([1, 2, 3]))

This is readable and communicates the contract directly.

If the function can accept multiple container types, encode that policy clearly instead of checking only for list out of habit.

Duck Typing Considerations

Python often favors duck typing over rigid type checks. If your function only needs iteration, you may not need a list check at all. A function that loops over items can often accept any iterable.

That said, explicit type checks are justified when mutability, indexing behavior, or a strict API contract matters.

Common Pitfalls

The most common mistake is using type(value) == list when isinstance(value, list) is actually the intended behavior. The strict type check rejects subclasses and is harder to extend.

Another pitfall is checking for Sequence when strings should not be accepted. If text input would be a bug, add a special-case exclusion for str and bytes.

A third issue is performing type checks where capability checks would be enough. Overly strict validation can make Python code less flexible than it needs to be.

Summary

  • Use isinstance(value, list) for most list checks in Python.
  • Use type(value) is list only when you require the exact built-in type.
  • Consider Sequence or other protocols when the function can accept broader input.
  • Beware that strings are sequences too.
  • Choose the check that matches the behavior your function actually needs.

Course illustration
Course illustration

All Rights Reserved.