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.