Check if all elements in a list are the same type in Python

avatar

Borislav Hadzhiev

Last updated: Aug 12, 2022

banner

Photo from Unsplash

Check if all elements in a list are the same type in Python #

Use the all() function to check if all elements in a list are the same type, e.g. if all(isinstance(item, str) for item in my_list):. The all() function will return True if all of the elements in the list are the same type and False otherwise.

main.py
my_list = ['Alice', 'Bob', 'Carl'] if all(isinstance(item, str) for item in my_list): # 👇️ this runs print('All elements in the list are strings') if all(isinstance(item, int) for item in my_list): print('All elements in the list are integers') if all(isinstance(item, float) for item in my_list): print('All elements in the list are floats')

We used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current item is of a specific type.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

main.py
print(isinstance(5, int)) # 👉️ True print(isinstance(3.14, float)) # 👉️ True print(isinstance('hello', str)) # 👉️ True print(isinstance(True, bool)) # 👉️ True print(isinstance([], list)) # 👉️ True

You can use the type() class to get the type of a value.

main.py
my_list = [123, 456, 789] # 👇️ <class 'int'> print(type(my_list[0]))

The type class returns the type of an object.

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

main.py
my_list = [123, 456, 789] if all(isinstance(item, int) for item in my_list): print('All elements in the list are integers')
If the all() function encounters an item that is not of the specified type, it will short-circuit returning False.

You can use a list comprehension if you need to filter out any values that are not of the specified type.

main.py
my_list = ['a', 123, 'b', 456, 789] only_integers = [item for item in my_list if isinstance(item, int)] # 👇️ [123, 456, 789] print(only_integers)

The list comprehension iterates over the list and filters out any non-integer values.

Note that the all() function returns True for any condition if the provided iterable is empty.

main.py
my_list = [] if all(isinstance(item, int) for item in my_list): # 👇️ this runs print('All elements in the list are integers') # 👇️ True print(all(isinstance(item, int) for item in my_list))

If you need to handle a scenario where the list is empty, check for its length.

main.py
my_list = [] if len(my_list) > 0 and all(isinstance(item, int) for item in my_list): print('All of the items in the list are integers') else: # 👇️ this runs print('Not all of the items in the list are integers')

We used the boolean and operator, so for the if block to run both conditions have to be met.

The list doesn't have a length greater than 0, so the else block runs.

If you need to check if the items in the list are all of the same type, regardless of the type, use the type() class.

main.py
my_list = [123, 456, 789] if all(isinstance(item, type(my_list[0])) for item in my_list): # 👇️ this runs print('All elements are of the same type')

The example checks if the type of the first list element is the same as the type of all of the other elements in the list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.