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'
Share:
107,077
FigNeutron
Author by

FigNeutron

Updated on July 19, 2021

Comments

  • FigNeutron
    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
    datashaman over 4 years
    This 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
    R. Cox over 3 years
    16 in [216] produces False; that is, @datashaman is correct; an int is not a string.
  • Bruce Bookman
    Bruce Bookman over 2 years
    if isinstance(x , int): print(x) Should be? if isinstance(i , int): print(i)