in operator
You can use the in
operator on list with the if
statement.
Let's discuss this with an example:
>>> Avengers= ['hulk', 'iron-man', 'Captain-America', 'Thor'] >>> if "iron-man" in Avengers: print "yes " yes >>> >>> if "vision" in Avengers: print "yes " >>>
In the preceding example, we check whether the particular value exists in the list or not. We check whether the strings, "iron-man"
and "vision"
, exist in the Avengers
list.
This way, you can use the in
operator in a list.