Can I use switch case alternative for below scenario?
If x
is any one of 5, 6, 7, 8
, I want to return 0
,
if x
is any one of 9, 10, 11, 12
, I want to return 1
,
if x
is any one of 13, 14, 15, 16
, I want to return 2
,
if x
is any one of 17, 18, 19, 20
, I want to return 3
.
I came across something like this for single value, but not sure how I can use similar syntax for above scenario.
def group(x):
return {
[5, 6, 7, 8]: 0,
[9, 10, 11, 12]: 1,
[13, 14, 15, 16]: 2,
[17, 18, 19, 20]: 3,
}.get(x, "Invalid")
Can someone please help me to conclude if I can use above way or only if else is the option?