python check if all values in list are integers

Python – Check If All Elements in List are Integers

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?

python check if all values in list are integers

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –


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


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top