Question: I'm refactoring some scripts that have a bunch of code with like this:

```
if 'mString1' in mStrVar:
    retVal = 1
elif 'mString2' in mStrVar:
    retVal = 2
elif 'mString3' in mStrVar:
    retVal = 3
elif 'mString4' in mStrVar:
    retVal = 4
else:
    retVal = 0

```

Personally I don't like this I always prefer the [`dict()`](https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python!) approach, but in this specific case, I think, I cannot be done in that way.

is it possible to rewrite this in a more short way? `mString` goes around `mString10` in some cases.

Any hints highly appreciated, and apologies if this is a duplicated I couldn't find any question related.

Answer: 
You could create a mapping list and use a plain for loop to search for the strings in the `mStrVar` variable:

```
retVal_mapping = [
   ('mString1', 1),
   ('mString2', 2),
   ('mString3', 2),
   ('mString4', 2),
   ('', 0) # default as "'' in anyString" is always true.
]

for s, retVal in retVal_mapping:
    if s in mStrVar:
        break

# retVal now contains the right value

```
```
mylist = ['mString1', 'mString2', 'mString3', 'mString4']
retVal = [i for i in range(len(mylist)) if mylist[i] = mStrVar][0] + 1

```