Sometimes, you need to know if a value in your Python program is really a list. Maybe you're building a function that should only work with lists, or you want to avoid errors when looping. Python makes it easy to check the type of an object. Here’s how you can write a simple, reusable function to check if something is a list, with clear explanations and examples.
01. Using isinstance()
(Recommended)
The isinstance()
function is the most Pythonic way to check if an object is a list. It returns True
if the object is a list, and False
otherwise.
def is_list(obj):
return isinstance(obj,list)
Example Usage:
print(is_list([1,2,3]))# True
print(is_list("hello"))# False
print(is_list((1,2,3)))# False
print(is_list(42))# False
Output:
True
False
False
False
Explanation:
-
isinstance(obj, list)
checks ifobj
is a list or a subclass of list. - Works even if you define your own class that inherits from
list
. - Recommended for most real-world Python code.
02. Using type()
for Exact Match
If you want to check that an object is exactly a list (not a subclass), you can use type()
:
def is_exact_list(obj):
return type(obj)islist
Example Usage:
print(is_exact_list([1,2,3]))# True
classMyList(list):
pass
mylist = MyList()
print(is_exact_list(mylist))# False
Output:
True
False
Explanation:
-
type(obj) is list
checks for the exact type, not subclasses. - Use this if you want to exclude custom list-like classes.
03. Function Examples and Outputs
Here’s a function that prints a message based on the type of the input:
def describe_object(obj):
if isinstance(obj,list):
print("It's a list!")
else:
print("Not a list.")
describe_object([10,20])
describe_object("Python")
describe_object({'a':1})
Output:
It's a list!
Not a list.
Not a list.
Explanation:
- The function uses
isinstance()
for flexible type checking. - Helps you debug or branch your code based on input type.
04. Handling Other Sequence Types
If you want your function to work with any sequence (like lists, tuples, or strings), you can check for collections.abc.Sequence
:
from collections.abc import Sequence
def is_sequence(obj):
return isinstance(obj, Sequence) and not isinstance(obj,str)
Example Usage:
print(is_sequence([1,2,3]))# True
print(is_sequence((1,2,3)))# True
print(is_sequence("abc"))# False (excluded string)
Output:
True
True
False
Explanation:
- This approach is useful if you want to accept lists and tuples, but not strings.
- Gives your code more flexibility for different sequence types.
05. Comparison Table: Ways to Check for a List in Python
Method | Checks Subclasses | Checks Only Lists | Best For |
---|---|---|---|
isinstance(obj, list) | Yes | No | General use, flexible code |
type(obj) is list | No | Yes | Exact type match |
collections.abc.Sequence | Yes | No | Accepting lists, tuples, etc. |
Conclusion
Checking if an object is a list in Python is simple and reliable. Use isinstance(obj, list)
for most cases, or type(obj) is list
if you want to be strict. For even more flexibility, check for sequence types using collections.abc.Sequence
. This helps you write safer, more robust, and more flexible Python code.