1

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
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Thehi198
  • 11
  • 1
  • Stack Overflow is not a place to request a function. Here you present your function which does not work as expected. – Klaus D. May 01 '21 at 04:01
  • 1
    @Klaus No, not all questions need to be debugging questions. How-to questions are perfectly fine. E.g. [How can I count the occurrences of a list item?](https://stackoverflow.com/q/2600191/4518341) – wjandrea May 01 '21 at 04:11
  • @wjandrea That's not a how-to question. – Klaus D. May 01 '21 at 04:14
  • 1
    @Klaus What do you mean? It doesn't literally say "how to", but you could easily rephrase it as "How to count the occurrences of a list item?" – wjandrea May 01 '21 at 04:15
  • @wjandrea Well, following that logic, any "question" trying to outsource homework is also a how-to question. Or in other words: the question here is missing any effort to solve the problem. – Klaus D. May 01 '21 at 04:21
  • 1
    @KlausD. The problem with do my homework questions is that they aren't generally applicable to multiple situations or future visitors. A simple task such as this question can be. – duckboycool May 01 '21 at 04:22
  • @duckboycool Exactly. See for example this Meta discussion: [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/4518341) – wjandrea May 01 '21 at 04:25

2 Answers2

-1
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))
Chetan Jain
  • 236
  • 6
  • 16
-2

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))
Dugbug
  • 454
  • 2
  • 11