Is in array (in)#
This keyword allows you to check an element in an array.
list#
test_list = [1,2,3,4,5]
print(5 in test_list)
print(6 in test_list)
True
False
tuple#
test_tuple = (1,2,3,4,5)
print(5 in test_tuple)
print(6 in test_tuple)
True
False
str#
test_str = "this is the test str contains subline to check it"
print("subline" in test_str)
print("subliner" in test_str)
True
False
dict#
Checks if the element on the left side of in
is in the keys (not values) of the dictionary on the right side of in
.
test_dict = {"a":1, "b":2}
print("a" in test_dict)
print(1 in test_dict)
True
False
not in
#
This operator returns True
if the collection from the right side of the operator contains the value from the left side of the operator, otherwise it returns False
.
Looks like it’s syntax sugar, because now I don’t see any cases where it differs from the expression not (<value> in <collection>)
. The following example supports this idea.
example_collection = ["a", "b", "c"]
print(not ("a" in example_collection))
print("a" not in example_collection)
False
False