daniel
daniel

Reputation: 143

simplify complex logical condition in python

How can I simplify this complex logical condition?

For a much shorter and more readable code

if i < 13:
    linux0[j] = query_res
elif 13 <= i < 27:
    linux1[j] = query_res
elif 27 <= i < 28:
    linux2[j] = query_res
elif 28 <= i < 33:
    linuxDnsServer1[j] = query_res
elif 33 <= i < 41:
    linuxRadius[j] = query_res
elif 41 <= i < 46:
    gen[j] = query_res
elif 46 <= i < 49:
    cdu[j] = query_res
elif 49 <= i < 55:
    avalanche[j] = query_res
elif 55 <= i < 69:
    defensePro_devices[j] = query_res

Upvotes: 3

Views: 103

Answers (1)

Barmar
Barmar

Reputation: 780974

It's not clear that it's more understandable, but you can put the limits and lists in another list, and iterate over it;

targets = [(12, linux0), (26, linux1), (27, linux2), (32, linuxDnsServer1),
           (40, linuxRadius), (45, gen), (48, cdu), (54, avalanche), (68, defensePro_devices)]
for limit, target in targets:
    if i <= limit:
        target[j] = query_res
        break

Upvotes: 4

Related Questions