In this tutorial, we will look at how to check if a list contains only numbers in Python with the help of some examples.
How to check if all list elements are numbers?
You can use a combination of the Python built-in
all() function to check if a list contains only numbers. For instance, you can use the following steps to check if all elements in a list are integers in Python –
- In a list comprehension, for each element in the list, check if it’s an integer using the
- Apply the Python built-in
all()function to return
Trueonly if all the items in the above list comprehension correspond to
The following is the code to check if all elements in a list are integers or not.
# check if all elements in ls are integers all([isinstance(item, int) for item in ls])
Let’s take a look at an example.
# list of numbers ls = [1, 2, 3, 4] # check if list contains only numbers print(all([isinstance(item, int) for item in ls]))
True as the output as all elements in the list
ls are integers.
Let’s take a look at another example.
# list of numbers and a string ls = [1, 2, 3, 4, 'cat'] # check if list contains only numbers print(all([isinstance(item, int) for item in ls]))
- 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
- 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
- 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.
Here we get
False as the output because not all elements in the list
ls are integers. One element, “cat” in the list is a string.
Check if all items in a list of strings are numeric
If you, however, have a list of strings and want to check whether all the elements in the list are digits or not, you can use the following code.
# list of numeric strings ls = ["1", "2", "3", "4"] # check if list of string contains only numberic elements print(all([item.isdigit() for item in ls]))
Here we check whether each element in the list of strings,
ls is a numerical value or not using the string
isdigit() function. We get
True as the output as all the strings in the list
ls are numeric characters.
You might also be interested in –
- Python – Check if String Contains Only Numbers
- Python – Check List Contains All Elements Of Another List
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.