Test if a variable is a list or tuple
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If you want to know whether a Python value is specifically a list or a tuple, the usual answer is isinstance(value, (list, tuple)). That check is simple, readable, and handles subclasses correctly. The harder part is deciding whether you truly want those two concrete types or whether you actually want to accept any sequence-like object.
The Basic Check
Python's isinstance accepts a tuple of types, so checking for either list or tuple is direct:
This prints:
That is the idiomatic answer when the question is literally about lists and tuples.
Why type(value) in (...) Is Usually Worse
You could write:
But that is stricter. It returns False for subclasses:
In most Python code, subclass-friendly behavior is desirable, so isinstance is preferred.
When You Might Want a Broader Check
Sometimes "list or tuple" is just shorthand for "something sequence-like." If that is the real requirement, checking only those two types can be too narrow.
For example, these are sequence-like in many contexts:
- '
range' - '
array.array' - '
numpy.ndarray' - custom sequence classes
If your function only needs indexing and length, you may want collections.abc.Sequence instead:
Notice the last line: strings are also sequences. That is often not what you want.
Excluding Strings and Bytes
When developers broaden a check to "sequence," they often accidentally accept text types. If a string should not count as a list-like input, exclude it explicitly.
This is a more useful check for many data-processing functions than the narrower list-or-tuple test.
Match the Check to the Intent
A good rule is:
- use
(list, tuple)when the code truly depends on those concrete container types - use a sequence protocol when the code depends on sequence behavior
For example, if your function mutates the input with append, then tuple input is not actually acceptable. In that case, checking for (list, tuple) is already too broad.
This is clearer than pretending tuples are supported when they are not.
Duck Typing Versus Type Checking
Python often encourages duck typing: try to use the object in the required way instead of checking its type first. That can be a better design when the operation is simple.
This approach is flexible, but it should be used intentionally. If the function contract really is "must be list or tuple," say so with isinstance.
Practical Recommendation
If the question came from a bug or validation rule, start by clarifying the real input contract. Many type checks are symptoms of an imprecise API boundary.
For most direct answers, this is enough:
Just be sure that concrete-type check matches what the rest of the function actually expects.
Common Pitfalls
- Using
type(value) is listwhen subclass-friendly behavior is desired. - Checking for list or tuple when the code really accepts any sequence-like object.
- Broadening the check to
Sequenceand accidentally accepting strings. - Accepting tuples in validation even though the function later mutates the container.
- Adding type checks where duck typing would make the interface simpler.
Summary
- '
isinstance(value, (list, tuple))is the standard way to test for either type.' - Prefer
isinstanceover directtype(...)comparisons in most cases. - Consider
collections.abc.Sequenceif the real requirement is broader than just list and tuple. - Exclude
strandbytesexplicitly when sequence checks should not treat text as list-like. - Make sure the type check matches what the function actually does with the value.
Related reading
- Test if lists share any items in python
- Test if numpy array contains only zeros
- Tetris-ing an array
- tf.SequenceExample with multidimensional arrays
- Test if executable exists in Python?
- 'tf' is not defined on load_model - using lambda
- The best shortest path algorithm
- The best way to calculate the height in a binary search tree? balancing an AVL-tree

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.