Check if all elements in a list are integers in Python

In this article, we will discuss different techniques to check if a list contains all integers only in Python.

Table Of Contents

Check if all elements in a list are integers using all() and isinstance()

Python provides a function isinstance() to check the type of an element. It accepts an element and a type as arguments, and returns True if the given element is of the given type only. We can use it to check if a given element is of int or not. For example,

num = 56
result = isinstance(num, int)

Here, isinstance() function will return True if the num variable is of int type, otherwise it will return False.

Then we can call this with each element of a list and collect the result in a boolean list. If all the elements in this boolean list are True then it means all elements in list are integers. The all() function accepts a sequence or list as argument and returns True if all the elements in given list are True. We can use the all() function to check if isinstance(element, int) returned True for each element of list or not. Let’s see an example,

Advertisements
listOfNumbers = [67, 78, 15, 14, 90, 98, 99, 76]

# Check if all elements in a list are integers Python
if all(isinstance(num, int) for num in listOfNumbers):
    print('Yes, all elements in List are integers')
else:
    print('No, all elements in List are not integers')

Output:

Yes, all elements in List are integers

Here, we confirmed that all elements in list are integers.

Check if all elements in a list are integers using for-loop

Instead of all() function, we can also use a simple for loop to check if all elements in a list are integers or not. Iterate over all elements of list using a for loop, and for each element call the isinstance(element, int) function to check if its of int type or not. As soon as you encounter an element that is not of int type, then mark the result as False and break the loop. But if all elements are integers in that list, then the result will be True. Let’s see an example,

listOfNumbers = [67, 78, 15, 14, 90, 98, 99, 76]

result = True

# Check if all elements in a list are integers Python
for num in listOfNumbers:
    if isinstance(num, int) == False:
        result = False
        break

# Check if all elements in a list are integers Python
if result:
    print('Yes, all elements in List are integers')
else:
    print('No, all elements in List are not integers')

Output:

Yes, all elements in List are integers

Here, we confirmed that all elements in list are integers.

Summary

We learned, how to check if all elements in a list are integers in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top