Use any() with isinstance()
In this step, you will learn how to use the any()
function in combination with the isinstance()
function to check if a list contains at least one element of a specific type. This is particularly useful when working with mixed-type lists.
The any()
function returns True
if any element in an iterable (like a list) is true. Otherwise, it returns False
. The isinstance()
function checks if an object is an instance of a specified class or type.
Let's create a Python script to demonstrate this. In the VS Code editor, create a new file named any_isinstance.py
in the ~/project
directory.
## Create a mixed-type list
my_list = [1, "hello", 3.14, True]
## Check if the list contains any integers
has_integer = any(isinstance(x, int) for x in my_list)
## Print the result
print("List contains an integer:", has_integer)
## Check if the list contains any strings
has_string = any(isinstance(x, str) for x in my_list)
## Print the result
print("List contains a string:", has_string)
## Check if the list contains any floats
has_float = any(isinstance(x, float) for x in my_list)
## Print the result
print("List contains a float:", has_float)
## Check if the list contains any booleans
has_bool = any(isinstance(x, bool) for x in my_list)
## Print the result
print("List contains a boolean:", has_bool)
Save the file. Now, execute the script using the python
command in the terminal:
python ~/project/any_isinstance.py
You should see the following output:
List contains an integer: True
List contains a string: True
List contains a float: True
List contains a boolean: True
In this example, we used a generator expression (isinstance(x, int) for x in my_list)
inside the any()
function. This generator expression yields True
if an element x
is an instance of int
, and False
otherwise. The any()
function then checks if any of these values are True
.
Let's modify the list to see how the output changes. Change the first element of my_list
to a float:
## Create a mixed-type list
my_list = [1.0, "hello", 3.14, True]
## Check if the list contains any integers
has_integer = any(isinstance(x, int) for x in my_list)
## Print the result
print("List contains an integer:", has_integer)
## Check if the list contains any strings
has_string = any(isinstance(x, str) for x in my_list)
## Print the result
print("List contains a string:", has_string)
## Check if the list contains any floats
has_float = any(isinstance(x, float) for x in my_list)
## Print the result
print("List contains a float:", has_float)
## Check if the list contains any booleans
has_bool = any(isinstance(x, bool) for x in my_list)
## Print the result
print("List contains a boolean:", has_bool)
Save the file and run it again:
python ~/project/any_isinstance.py
The output should now be:
List contains an integer: False
List contains a string: True
List contains a float: True
List contains a boolean: True
Now, the list does not contain any integers, so has_integer
is False
.
This technique is useful for validating the contents of a list or for performing different actions based on the types of elements present in the list.