I am a beginner in python, I am looking for a function that can tell me the number of integers in a given list. For example:
list = [1, 2, 4.5, 6, 8.3, 9]
print(int_num(list))
output
4
I am a beginner in python, I am looking for a function that can tell me the number of integers in a given list. For example:
list = [1, 2, 4.5, 6, 8.3, 9]
print(int_num(list))
output
4
def isInt(x):
if isinstance(x, int):
return True
else:
return False
ls = [1,2,4.5,6,8.3,9]
# Note filter function is used to filter out elements that do not satisy the function in our case it is isInt
# filter function gives a filter object cast it to a list
filtered = list(filter(isInt, ls))
print(filtered)
print(len(filtered))
There is already a built-in function len()
, that, if required, can be wrapped in your own custom function
list = [1, 2, 3, 4]
print(len(list))