python "set" and "in"

54 Views Asked by At

I've got a little problem with set and in operator in python.Why is the index "in" the set, but not the actual value? The code is following:

sett = {}

sett[10]="a"
sett[12]="b"
sett[31]="c"

print('a' in sett)
print(31 in sett)

output: False True

1

There are 1 best solutions below

0
On

When using "in" for dictionaries, it is going to search through the keys, not the the values. The dictionary is a series of key:value sets. So the there is no "a" key, but there is a "31" key. If you try:

print('a' in sett[10])

This will return True because now it is searching the value for a specific key.