All built-in collections in Python implement a way to check element membership using in
.
alist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5 in alist # True
10 in alist # False
atuple = ('0', '1', '2', '3', '4')
4 in atuple # False
'4' in atuple # True
astring = 'i am a string'
'a' in astring # True
'am' in astring # True
'I' in astring # False
aset = {(10, 10), (20, 20), (30, 30)}
(10, 10) in aset # True
10 in aset # False
dict
is a bit special: the normal in
only checks the keys. If you want to search in values you need to specify it. The same if you want to search for key-value pairs.
adict = {0: 'a', 1: 'b', 2: 'c', 3: 'd'}
1 in adict # True - implicitly searches in keys
'a' in adict # False
2 in adict.keys() # True - explicitly searches in keys
'a' in adict.values() # True - explicitly searches in values
(0, 'a') in adict.items() # True - explicitly searches key/value pairs