Skip to Content

Python – Check If All List Elements are of the same Type

Lists are a very versatile data structure in Python used to store ordered one-dimensional data and can store data of different types together. In this tutorial, we will look at how to check if all the elements in a list have the same type or not in Python with the help of some examples.

How to check if all the list items have the same type?

You can use the Python built-in all() function to check if all the elements in a list are of the same type by checking if the type of each value is the same as that of the first value in the list.

python check if all list elements are of same type

The all() function takes in an iterable as an argument and returns True if all the values in the iterable are truthy (represent True in a boolean context).

So, to check if all the values in a given list are of the same type or not, use the all() function to check if the type of each value in the list is the same as that of the first value in the list. The following is the syntax –

# check if all the list values are of the same type
all(type(val) == type(ls[0]) for val in ls)

It returns True if all the values in the list have the same type.

Examples

Let’s now look at some examples of using the above methods. First, we will create a few lists that we’ll use to demonstrate the methods.

# list with int values
ls1 = [1, 2, 3, 4, 5]
# list with string values
ls2 = ['you', 'are', 'my', 'fire']
# list with values of different types
ls3 = [14, 'cat', 12, True, 2.7]


# display the lists
print(ls1)
print(ls2)
print(ls3)

Output:

[1, 2, 3, 4, 5]
['you', 'are', 'my', 'fire']
[14, 'cat', 12, True, 2.7]

Here, we created three lists – ls1, ls2, and ls3. The list ls1 contains only integer values as its elements, the list ls2 has only string elements and the list ls3 contains elements of different types.

Example 1 – Check if all the list elements are of the same type using all() and type()

The idea here is to use the all() function to check if the type of each list element is equal to the type of the first list element. You can use the Python built-in type() function to get the type of an object.

You can use a list comprehension to create a list of boolean values – whether the type of a list element is the same as that of the first list value and then pass this resulting list as an argument to the all() function.

Let’s apply this to the lists created above.

# check if all list values are of the same type
print(all([type(val) == type(ls1[0]) for val in ls1]))
print(all([type(val) == type(ls2[0]) for val in ls2]))
print(all([type(val) == type(ls3[0]) for val in ls3]))

Output:

True
True
False

We get True for ls1 and ls2 as they both have their respective values of the same type. We get False for ls3 as it contains values of different types.

Note that the all() function takes an iterable as an argument, you can directly use an iterator (without using a list comprehension).

# check if all list values are of the same type
print(all(type(val) == type(ls1[0]) for val in ls1))
print(all(type(val) == type(ls2[0]) for val in ls2))
print(all(type(val) == type(ls3[0]) for val in ls3))

Output:

True
True
False

We get the same results as above.

Example 2 – List with int and float values

Let’s see what happens if a list contains a mix of integer and float values.

# list with int and float values
ls4 = [1, 2, 3, 4.6, 5.1]

# check if all list values are of the same type
print(all(type(val) == type(ls4[0]) for val in ls4))

Output:

False

We get False as the output since the type of values in the list here is different. int and float are not the same and hence, here, we say that all the values in the list are not of the same type.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.