Check if an input is equal to any item in the list [duplicate]
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?
python
marked as duplicate by slider, quamrana, Austin, Rahul K P, Mr. T Nov 22 '18 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.
add a comment |
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?
python
marked as duplicate by slider, quamrana, Austin, Rahul K P, Mr. T Nov 22 '18 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 doif x in a: return True
.
– slider
Nov 22 '18 at 18:06
it is not a duplicate of that article as i have a different situation
– Coder Cody
Nov 22 '18 at 18:12
How is it different? Can you be more specific?
– slider
Nov 22 '18 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 '18 at 18:17
1
@slider Justx in a
will do the trick.
– Klaus D.
Nov 22 '18 at 19:11
add a comment |
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?
python
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
python
asked Nov 22 '18 at 18:05
Coder CodyCoder Cody
408
408
marked as duplicate by slider, quamrana, Austin, Rahul K P, Mr. T Nov 22 '18 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 '18 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 doif x in a: return True
.
– slider
Nov 22 '18 at 18:06
it is not a duplicate of that article as i have a different situation
– Coder Cody
Nov 22 '18 at 18:12
How is it different? Can you be more specific?
– slider
Nov 22 '18 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 '18 at 18:17
1
@slider Justx in a
will do the trick.
– Klaus D.
Nov 22 '18 at 19:11
add a comment |
You can doif x in a: return True
.
– slider
Nov 22 '18 at 18:06
it is not a duplicate of that article as i have a different situation
– Coder Cody
Nov 22 '18 at 18:12
How is it different? Can you be more specific?
– slider
Nov 22 '18 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 '18 at 18:17
1
@slider Justx in a
will do the trick.
– Klaus D.
Nov 22 '18 at 19:11
You can do
if x in a: return True
.– slider
Nov 22 '18 at 18:06
You can do
if x in a: return True
.– slider
Nov 22 '18 at 18:06
it is not a duplicate of that article as i have a different situation
– Coder Cody
Nov 22 '18 at 18:12
it is not a duplicate of that article as i have a different situation
– Coder Cody
Nov 22 '18 at 18:12
How is it different? Can you be more specific?
– slider
Nov 22 '18 at 18:14
How is it different? Can you be more specific?
– slider
Nov 22 '18 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 '18 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 '18 at 18:17
1
1
@slider Just
x in a
will do the trick.– Klaus D.
Nov 22 '18 at 19:11
@slider Just
x in a
will do the trick.– Klaus D.
Nov 22 '18 at 19:11
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 22 '18 at 18:24
answered Nov 22 '18 at 18:12
Andrey SuglobovAndrey Suglobov
1349
1349
add a comment |
add a comment |
You can do
if x in a: return True
.– slider
Nov 22 '18 at 18:06
it is not a duplicate of that article as i have a different situation
– Coder Cody
Nov 22 '18 at 18:12
How is it different? Can you be more specific?
– slider
Nov 22 '18 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 '18 at 18:17
1
@slider Just
x in a
will do the trick.– Klaus D.
Nov 22 '18 at 19:11