Search code examples
pythonlistfor-loopint

If N is equal to an integer


I'm currently writing a function that runs through a list of elements, and only does operation on list elements that are integers. It looks like this:

for n in list1:
   if n == int:
         #Do stuff

What I'm struggling with is how to actually write out the loop to detect if the element is an integer. What should I do for this? I can't find anything in the docs of Python (Although maybe I haven't looked deep enough in).

Thanks for any help.


Solution

  • Use the isinstance() function:

    for n in list1:
       if isinstance(n, int):
           # Do stuff