How to check if a specific integer is in a list
107,077
Are you looking for this?:
if n in my_list:
---do something---
Where n
is the number you're checking. For example:
my_list = [1,2,3,4,5,6,7,8,9,0]
if 1 in my_list:
print 'True'
Author by
FigNeutron
Updated on July 19, 2021Comments
-
FigNeutron almost 3 years
I want to know how to make an if statement that executes a clause if a certain integer is in a list.
All the other answers I've seen ask for a specific condition like prime numbers, duplicates, etc. and I could not glean the solution to my problem from the others.
-
datashaman over 4 yearsThis answer is wrong. The first stanza produces: Test 1 False, Test 2 False, as one would expect intuitively from python. You should remove this answer, it makes no sense.
-
R. Cox over 3 years16 in [216] produces False; that is, @datashaman is correct; an int is not a string.
-
Bruce Bookman over 2 yearsif isinstance(x , int): print(x) Should be? if isinstance(i , int): print(i)