If you want to see a specific number then you use in
keyword but let say if you have
list2 = ['a','b','c','d','e','f',1,2,3,4,5,6,7,8,9,0]
Then what you can do. You simply do this:
list2 = ['a','b','c','d','e','f',1,2,3,4,5,6,7,8,9,0]
for i in list2:
if isinstance[x , int]:
print[x]
this will only print integer which is present in a given list.
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 isinstance[]
and 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
isinstance[]
function. - Apply the Python built-in
all[]
function to returnTrue
only if all the items in the above list comprehension correspond toTrue
.
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]]]
Output:
True
We get 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]]]
Output:
False
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]]]
Output:
True
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.
Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.
View all posts
This article describes how to check if a number is an integer or a decimal in Python.
- Check if object is
int
orfloat
:isinstance[]
- Check if
float
is integer:is_integer[]
- Check if numeric string is integer
See the following article for how to get the fractional and integer parts.
- Get the fractional and integer parts with math.modf[] in Python
See the following article for checking if a string is a number.
- Check if a string is numeric, alphabetic, alphanumeric, or ASCII
Check if object is int
or float
: isinstance[]
You can get the type of an object with the built-in function type[]
.
i = 100
f = 1.23
print[type[i]]
print[type[f]]
#
#
You can also check if an object is of a particular type
with the built-in function isinstance[object, type]
.
- Get and check the type of an object in Python: type[], isinstance[]
print[isinstance[i, int]]
# True
print[isinstance[i, float]]
# False
print[isinstance[f, int]]
# False
print[isinstance[f, float]]
# True
In this case, since only the type is checked, you cannot check if the value of float
is an integer [the fractional part is 0].
f_i = 100.0
print[type[f_i]]
#
print[isinstance[f_i, int]]
# False
print[isinstance[f_i, float]]
# True
Check if float
is integer: is_integer[]
float
has is_integer[]
method that
returns True
if the value is an integer, and False
otherwise.
- Built-in Types - float.is_integer — Python 3.10.5 documentation
f = 1.23
print[f.is_integer[]]
# False
f_i = 100.0
print[f_i.is_integer[]]
# True
For example, a function that returns True
for an integer number [int
or integer float
] can be defined as follows. This function returns False
for str
.
def is_integer_num[n]:
if isinstance[n, int]:
return True
if isinstance[n, float]:
return n.is_integer[]
return False
print[is_integer_num[100]]
# True
print[is_integer_num[1.23]]
# False
print[is_integer_num[100.0]]
# True
print[is_integer_num['100']]
# False
Check if numeric string is integer
If you want to determine that the string of integer numbers is also an integer, the following function can be used.
If possible, the value is converted to float
with float[]
, then is_integer[]
method is called, and the result is returned.
def is_integer[n]:
try:
float[n]
except ValueError:
return False
else:
return float[n].is_integer[]
print[is_integer[100]]
# True
print[is_integer[100.0]]
# True
print[is_integer[1.23]]
# False
print[is_integer['100']]
# True
print[is_integer['100.0']]
# True
print[is_integer['1.23']]
# False
print[is_integer['string']]
# False
See the following articles for details on converting strings to numbers and handling exceptions with try ... except ...
.
- Convert a string to a number [int, float] in Python
- "try ... except ... else ... finally ..." in Python