Yasuoredarmy
Yasuoredarmy

Reputation: 9

python "set" and "in"

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

Upvotes: 0

Views: 49

Answers (1)

Holden
Holden

Reputation: 642

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.

Upvotes: 1

Related Questions