Python List Membership Test
Using “in” and “not in”
We can test if an item exists in a list or not, using the keyword “in” or “not in”.
Example of “in”
n1=['c','o','m','p','u','t','e','r'] print(n1) print('c' in n1) print('o' in n1) print('p' in n1) print('t' in n1) print('w' in n1) print('q' in n1)
Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
True
True
True
True
False
False
Example of not in
n1=['c','o','m','p','u','t','e','r'] print(n1) print('c' not in n1) print('o' not in n1) print('p' not in n1) print('t' not in n1) print('w' not in n1) print('q' not in n1)
Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
False
False
False
False
True
True