0

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.

pixel rain
  • 75
  • 2
  • 8

2 Answers2

1

Use the isinstance() function:

for n in list1:
   if isinstance(n, int):
       # Do stuff
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0
for n in list1:
    if isinstance( n, ( int, long )):
         #dostuff
EralpB
  • 1,621
  • 4
  • 23
  • 36
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Mar 28 '17 at 21:23