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.
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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 –
- Python – Check If All Elements in a List are Equal
- Python – Check If All Elements in a List are Unique
- Check If a List Contains Only Numbers in Python
- Python – Check List Contains All Elements Of Another List
- Python – Check if an element is in a list
- Python – Check If List Is Sorted Or Not
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.