Check if an input is equal to any item in the list [duplicate]












0















This question already has an answer here:




  • Fastest way to check if a value exist in a list

    12 answers




let's say we have a list



a=["a1", "a2", "a3"]


then we have an input:



x=input("enter something: ")


how would you check if that input "x" is equal to any item in the list and then return true?










share|improve this question













marked as duplicate by slider, quamrana, Austin, Rahul K P, Mr. T Nov 22 at 20:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You can do if x in a: return True.
    – slider
    Nov 22 at 18:06










  • it is not a duplicate of that article as i have a different situation
    – Coder Cody
    Nov 22 at 18:12










  • How is it different? Can you be more specific?
    – slider
    Nov 22 at 18:14










  • I didn't really want to disclose this information but I'm trying to make a naughts and crosses game and I've created lists to hold the data of each square in the naughts and crosses. I will then ask for an input and use that input, not to count how many times it repeats but use it to simply see whether the input correlates to a keyword to describe each square. I just did not find my answer on that particular post
    – Coder Cody
    Nov 22 at 18:17






  • 1




    @slider Just x in a will do the trick.
    – Klaus D.
    Nov 22 at 19:11
















0















This question already has an answer here:




  • Fastest way to check if a value exist in a list

    12 answers




let's say we have a list



a=["a1", "a2", "a3"]


then we have an input:



x=input("enter something: ")


how would you check if that input "x" is equal to any item in the list and then return true?










share|improve this question













marked as duplicate by slider, quamrana, Austin, Rahul K P, Mr. T Nov 22 at 20:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You can do if x in a: return True.
    – slider
    Nov 22 at 18:06










  • it is not a duplicate of that article as i have a different situation
    – Coder Cody
    Nov 22 at 18:12










  • How is it different? Can you be more specific?
    – slider
    Nov 22 at 18:14










  • I didn't really want to disclose this information but I'm trying to make a naughts and crosses game and I've created lists to hold the data of each square in the naughts and crosses. I will then ask for an input and use that input, not to count how many times it repeats but use it to simply see whether the input correlates to a keyword to describe each square. I just did not find my answer on that particular post
    – Coder Cody
    Nov 22 at 18:17






  • 1




    @slider Just x in a will do the trick.
    – Klaus D.
    Nov 22 at 19:11














0












0








0








This question already has an answer here:




  • Fastest way to check if a value exist in a list

    12 answers




let's say we have a list



a=["a1", "a2", "a3"]


then we have an input:



x=input("enter something: ")


how would you check if that input "x" is equal to any item in the list and then return true?










share|improve this question














This question already has an answer here:




  • Fastest way to check if a value exist in a list

    12 answers




let's say we have a list



a=["a1", "a2", "a3"]


then we have an input:



x=input("enter something: ")


how would you check if that input "x" is equal to any item in the list and then return true?





This question already has an answer here:




  • Fastest way to check if a value exist in a list

    12 answers








python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 18:05









Coder Cody

408




408




marked as duplicate by slider, quamrana, Austin, Rahul K P, Mr. T Nov 22 at 20:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by slider, quamrana, Austin, Rahul K P, Mr. T Nov 22 at 20:09


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • You can do if x in a: return True.
    – slider
    Nov 22 at 18:06










  • it is not a duplicate of that article as i have a different situation
    – Coder Cody
    Nov 22 at 18:12










  • How is it different? Can you be more specific?
    – slider
    Nov 22 at 18:14










  • I didn't really want to disclose this information but I'm trying to make a naughts and crosses game and I've created lists to hold the data of each square in the naughts and crosses. I will then ask for an input and use that input, not to count how many times it repeats but use it to simply see whether the input correlates to a keyword to describe each square. I just did not find my answer on that particular post
    – Coder Cody
    Nov 22 at 18:17






  • 1




    @slider Just x in a will do the trick.
    – Klaus D.
    Nov 22 at 19:11


















  • You can do if x in a: return True.
    – slider
    Nov 22 at 18:06










  • it is not a duplicate of that article as i have a different situation
    – Coder Cody
    Nov 22 at 18:12










  • How is it different? Can you be more specific?
    – slider
    Nov 22 at 18:14










  • I didn't really want to disclose this information but I'm trying to make a naughts and crosses game and I've created lists to hold the data of each square in the naughts and crosses. I will then ask for an input and use that input, not to count how many times it repeats but use it to simply see whether the input correlates to a keyword to describe each square. I just did not find my answer on that particular post
    – Coder Cody
    Nov 22 at 18:17






  • 1




    @slider Just x in a will do the trick.
    – Klaus D.
    Nov 22 at 19:11
















You can do if x in a: return True.
– slider
Nov 22 at 18:06




You can do if x in a: return True.
– slider
Nov 22 at 18:06












it is not a duplicate of that article as i have a different situation
– Coder Cody
Nov 22 at 18:12




it is not a duplicate of that article as i have a different situation
– Coder Cody
Nov 22 at 18:12












How is it different? Can you be more specific?
– slider
Nov 22 at 18:14




How is it different? Can you be more specific?
– slider
Nov 22 at 18:14












I didn't really want to disclose this information but I'm trying to make a naughts and crosses game and I've created lists to hold the data of each square in the naughts and crosses. I will then ask for an input and use that input, not to count how many times it repeats but use it to simply see whether the input correlates to a keyword to describe each square. I just did not find my answer on that particular post
– Coder Cody
Nov 22 at 18:17




I didn't really want to disclose this information but I'm trying to make a naughts and crosses game and I've created lists to hold the data of each square in the naughts and crosses. I will then ask for an input and use that input, not to count how many times it repeats but use it to simply see whether the input correlates to a keyword to describe each square. I just did not find my answer on that particular post
– Coder Cody
Nov 22 at 18:17




1




1




@slider Just x in a will do the trick.
– Klaus D.
Nov 22 at 19:11




@slider Just x in a will do the trick.
– Klaus D.
Nov 22 at 19:11












1 Answer
1






active

oldest

votes


















1














You can check it with that code:



print("Yes, x in list" if x in a else "No, x is not in list")


It same that:



if x in a:
print("Yes, x in list")
else:
print("No, x is not in list")


Also if you want to get index of contained element - just use .index():



if x in a: 
print('Yes, x in list, and it index is:', a.index(x))


In that example i'am avoid else: construct, but you can use it like in previous example, if you need it.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can check it with that code:



    print("Yes, x in list" if x in a else "No, x is not in list")


    It same that:



    if x in a:
    print("Yes, x in list")
    else:
    print("No, x is not in list")


    Also if you want to get index of contained element - just use .index():



    if x in a: 
    print('Yes, x in list, and it index is:', a.index(x))


    In that example i'am avoid else: construct, but you can use it like in previous example, if you need it.






    share|improve this answer




























      1














      You can check it with that code:



      print("Yes, x in list" if x in a else "No, x is not in list")


      It same that:



      if x in a:
      print("Yes, x in list")
      else:
      print("No, x is not in list")


      Also if you want to get index of contained element - just use .index():



      if x in a: 
      print('Yes, x in list, and it index is:', a.index(x))


      In that example i'am avoid else: construct, but you can use it like in previous example, if you need it.






      share|improve this answer


























        1












        1








        1






        You can check it with that code:



        print("Yes, x in list" if x in a else "No, x is not in list")


        It same that:



        if x in a:
        print("Yes, x in list")
        else:
        print("No, x is not in list")


        Also if you want to get index of contained element - just use .index():



        if x in a: 
        print('Yes, x in list, and it index is:', a.index(x))


        In that example i'am avoid else: construct, but you can use it like in previous example, if you need it.






        share|improve this answer














        You can check it with that code:



        print("Yes, x in list" if x in a else "No, x is not in list")


        It same that:



        if x in a:
        print("Yes, x in list")
        else:
        print("No, x is not in list")


        Also if you want to get index of contained element - just use .index():



        if x in a: 
        print('Yes, x in list, and it index is:', a.index(x))


        In that example i'am avoid else: construct, but you can use it like in previous example, if you need it.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 18:24

























        answered Nov 22 at 18:12









        Andrey Suglobov

        1248




        1248















            Popular posts from this blog

            How to ignore python UserWarning in pytest?

            Script to remove string up to first number

            Service Worker FechtEvent.respondWith response is null on iOS 12.1 Safari