Map set to a value if it is a subset of a key set

18 hours ago 4
ARTICLE AD BOX

if I have the following code:

if value == key1: return value1 elif value == key2: return value2 ...

I can simplify it using a dict, obviously

d = { key1: value1, key2: value2, ...} return d[value]

Right now, I have something similar with sets:

if value in { key1, key2, key3...}: return value1 elif value in { key4, key5, key6...}: return value2 ...

I was wondering if there is a way to simplify this, or is the if-elif chain the best? I could do something with a dict from frozensets to the values and find the first one to which value belongs using next and a generator expression, but that is getting way too "clever".

Note I'm not looking for a one-liner necessarily, just if there is a better pattern for this.

Read Entire Article