How to check if a given object is a list or not in Python? In python, there are several ways to check if the given object is a list or not. When you use these methods, you need to specify what type you are checking with and if the object is that specific type it returns True or else Flase. In our case, we need to specify the list as an argument.
The following are methods to check if an object is a list or not
- Using isinstance()
- Using isinstance() with typing.List
- Using type()
- Using __class__
1. Quick Examples to Check Whether Object is List or Not
Following are quick examples to check if the object is a List or not in python.
# Below are some quick examples.
# Using isinstance()
if (isinstance(countries, list)):
print("List")
else:
print("not a List")
# Using isinstance() with typing.List
if (isinstance(countries, typing.List)):
print("List")
else:
print("not a List")
# Using type()
if (type(countries) is list):
print("List")
else:
print("not a List")
# Using __class__
if (countries.__class__ == list):
print("List")
else:
print("not a List")
2. Using isinstance() to Check If Object is List in Python
The isinstance() method in python is used to check if the given object is a list or not. Actually, this method is used to check if object of a specific type, not just lists. This method takes two parameters first, the object, and second the type you wanted to check with. If the input object is of a specified type, it returns True, otherwise False is returned.
Here, we will pass our object as the first parameter and list as the second parameter. Let’s see how to use isinstance() method.
# Syntax
isinstance(object, list)
Let’s create two objects, one is a string and another is a list of countries. We will check if both are list objects or not using isinstance() method.
countries=['Russia', 'Japan', 'Mexico', 'Europe']
city='Guntur'
# Using isinstance() to check countries is list
if (isinstance(countries, list)):
print("List")
else:
print("not a List")
# Using isinstance() to check city is list
if (isinstance(city, list)):
print("List")
else:
print("not a List")
Yields below output.
We can see that countries is a List type and city is not a list.
3. Using isinstance() with typing.List
It is similar to the above method. But, we will pass typing List as a second parameter to the isinstance() method instead of list. Here, typing is a module that we need to import.
Let’s see how to use isinstance()
method with typing.List.
# Syntax
isinstance(object, typing.List)
Let’s use two objects. One is a string and another is a list of countries. We will check if both are list objects or not using isinstance
() method.
import typing
# Using isinstance() with typing.list to check countries is list
if (isinstance(countries, typing.List)):
print("List")
else:
print("not a List")
# Using isinstance() with typing.list to check city is list
if (isinstance(city, typing.List)):
print("List")
else:
print("not a List")
Yields the same output as above.
4. Check If Given Object is List Using type()
type()
will return the type of the object. Here, we will compare the object returning type with the list using is operator. If the object is a list, True
is returned, Otherwise False
is returned. Let’s see the syntax of type() with is operator.
# Syntax
type(object) is list
Let’s use it with an example.
# Using type() to check countries is list
if (type(countries) is list):
print("List")
else:
print("not a List")
# Using type() to check city is list
if (type(city) is list):
print("List")
else:
print("not a List")
# Output:
# List
# not a List
5. Using __class__
In Python __class__
will return the class of the given object. Here, we will compare the object with list using ==
operator. If the object is list, True
is returned, Otherwise False
is returned. Let’s see an example
# Using __class__ to check countries is list
if (countries.__class__ == list):
print("List")
else:
print("not a List")
# Using _class_ to check city is list
if (city.__class__ == list):
print("List")
else:
print("not a List")
# Output:
# List
# not a List
6. Conclusion
We have seen several ways to check if the object is a list or not in python using isinstance()
, and isinstance()
with typing.List
, type()
and __class__
. In all these methods, we used a if
statement that will print a statement specifying whether the object is a list or not.
With most of these methods, you need to specify what type you are checking with and if the object is that specific type it returns True or else Flase. In our case, we need to specify the list as an argument.
Related Articles
- Python Check if Key Exists in Dictionary
- Python Check if File Exists
- Check If the Set is Empty in Python
- How can I check for NaN values in Python?
- How to Check If a Python Dictionary is Empty
- How to check Python Version?
- Python Type Check with Examples
- Check if a String is a Float in Python
- Check if the List is Empty in Python
- Python List Contains – Check if Element Exists in List
- Check if String is Empty or Not in Python
- Print Object Properties and Values in Python?
- Explain Classes & Objects in Python
- Check object has an attribute in Python
- Python Functions Explained
- Check Two Sets are Equal in Python