Reduced if/else block

python

Solution

You can use a dictionary:

var_map = {1: 'foo', 2: 'bar', 10: 'spam'}
var = var_map[a]

If `a` is a sequential integer, you can use a list too:

var_map = [None, 'some_value', 'another_one', 'text_string', 'one_more', 'final_str']
var = var_map[a]

Here `var_map[0]` is set to `None` to keep the sequence map simple.

Problem

I'm trying to assign a value to a variable according to some input. This is what I currently do: ``` if a == 1: var = 'some_value' elif a == 2: var = 'another_one' elif a == 3: var = 'text_string' elif a == 4: var = 'one_more' elif a == 5: var = 'final_str' ``` So basically it maps a given value to `var` according to the value of `a`. Could this be pythonified somehow?

Original source