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 are integers or not in Python with the help of some examples.
How to check if all the list items are integers?
You can use the Python built-in isinstance()
function to check whether an object is of a particular data type or not. For example, to check if a value is of int
type or not, use isinstance(val, int)
.
But, the above function checks for only a single value. What if you want to know whether all the values in a list are of int
type?
Use the Python built-in all()
function.
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 int
type or not, use the all()
function to check if each value in the list is of int
type (using the isinstance()
function). The following is the syntax –
# check if all the list values are of int type all(isinstance(val, int) for val in ls)
It returns True
if all the values in the list are of int
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.
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.
# 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.
The idea here is to use the all()
function to check if the type of each list element is int
or not. You can use the Python built-in isinstance()
function to check if a list value is of int
type or not.
You can use a list comprehension to create a list of boolean values – whether a list element is an integer or not 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 integers print(all([isinstance(val, int) for val in ls1])) print(all([isinstance(val, int) for val in ls2])) print(all([isinstance(val, int) for val in ls3]))
Output:
True False False
We get True
for ls1
as it contains only integer values and False
for both ls2
and ls3
as all the values in these respective lists are not integers.
Note that the all()
function takes an iterable as an argument, so you can directly use an iterator (without using a list comprehension).
# check if all list values are integers print(all(isinstance(val, int) for val in ls1)) print(all(isinstance(val, int) for val in ls2)) print(all(isinstance(val, int) for val in ls3))
Output:
True False False
We get the same results as above.
Let’s see what happens if we apply the above method to an empty list.
# an empty list ls4 = [] # check if all list values are of integers print(all(isinstance(val, int) for val in ls4))
Output:
True
We get True
as the output.
You might also be interested in –
- Python – Check If All Elements in List are None
- Python – Check If All Elements in List are Zero
- 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.