Python: if/else statement vs dictionary

Chuan-Yen Chiang
2 min readApr 5, 2018

One day, my friend posted a message on his Plurk about coding style of if/else statement. The story is, he wants to know how elegant to use “if/else” instead of use “if/else if/else” statement. For example, if we want to implement different operator based on specific element of JSON file.

Here is the json file looks like.
{
"ops": "add",
"value_1": 1,
"value_2": 2
},
{
"ops": "sub",
"value_1": 1,
"value_2": 2
},
{
"ops": "mul",
"value_1": 1,
"value_2": 2
},
{
"ops": "div",
"value_1": 1,
"value_2": 2
}

Here is the original code style of my friend.

if data["ops"] == "add":
ans = data["value_1"] + data["value_2"]
elif data["ops"] == "sub":
ans = data["value_1"] - data["value_2"]
elif data["ops"] == "mul":
ans = data["value_1"] * data["value_2"]
elif data["ops"] == "div":
ans = data["value_1"] / data["value_2"]
else:
# pass

And I have different opinion of that code. I think the “elif” is a little bit “ugly” why don’t you just use “if” statement? Because we only need to deal with the operation we have provided. So, the code looks like this.

if data["ops"] == "add":
ans = data["value_1"] + data["value_2"]
if data["ops"] == "sub":
ans = data["value_1"] - data["value_2"]
if data["ops"] == "mul":
ans = data["value_1"] * data["value_2"]
if data["ops"] == "div":
ans = data["value_1"] / data["value_2"]

But, after a while, I came out with different idea, why do I just use lambda and dictionary to create a key-value map for the specific operator? So, the code looks like this.

operators = {
"add": lambda x: x["value_1"] + x["value_2"],
"sub": lambda x: x["value_1"] - x["value_2"],
"mul": lambda x: x["value_1"] * x["value_2"],
"div": lambda x: x["value_1"] / x["value_2"]
}
ans = operators[data["ops"]](data)

After I provided my new idea to my friend and he says how about the else part? What if the data contains the operator that doesn’t existed in “ops” element? Yes, my code will failed. And I said, use try/except to catch exceptions and deal with that.

try:
ans = operators[data["ops"]](data)
except KeyError:
print("ops: {ops} not support".format(ops=data["ops"]))

But, how about we really want to add if/else statement into our code, said my friend. Well, let’s do it!

if data["ops"] in operators.keys():
ans = operators[data["ops"]](data)
else:
print("ops: {ops} not support".format(ops=data["ops"]))

Let’s compare the performance of those implementation! Code is available in github.

time_if_else_statement: 22.46713638305664 ms
time_if_elif_statement: 17.323017120361328 ms
time_try_except_dict_func_statement: 24.687767028808594 ms
time_if_else_dict_func_statement: 28.36775779724121 ms

The results are kind of blow up my mind. The if/elif statement is much faster than other else when we set amount of test case into 50000. It’s kind of trade-off between performance and readable code. For me? I will choose try_except_dict_func_statement for my code of similar situation. Easy to understand and maintain.

--

--