Checking if an element is in a list in Python
To check if an element is in the list, we can use the in
operator. In this case, if the element is present, we will get back the Boolean value True
, otherwise - False
:
lst = [1, 2, 3]
res = 1 in lst
print(res) # True
Given a list:
lst = ['a', 'b', 'c', 'd', 'e']
Check for an element with value 'c'
.
The following code is given:
lst = ['a', 'b', 'c', 'd']
res = 'e' in lst
print(res)
Tell me what will be output to the console.
The following code is given:
lst = ['a', 1, 'b', 3, 'c']
res = 3 in lst
print(res)
Tell me what will be output to the console.