Select IP Address from List

ip = system.tag.read(’[System]Client/Network/IPAddress’)
if ip == ‘10.10.1.100’ or ‘10.10.1.101’ or ‘10.10.1.102’:
‘do something’

I know there’s a way to shorten the if statement to search through a list to see if the ip address matches one in the list. I can’t remember how. Any help would be awesome!

if ip in ['1.2.3.4', '1.2.3.5', '1.2.3.6']:
    # do something with ip
1 Like

In addition to what @Kevin.Herron said, a general programming technique I use for situations like this that might branch off into other functions would be to map the input (in your case the IP) to different functions with a dictionary. So something like

 ip_function_map = {
    '1.2.3.4': someModule.foo,
    '1.2.3.5': otherModule.bar,
    '1.2.3.6': anotherModule.fooBar
}

ip = system.tag.read('[System]Client/Network/IPAddress')
try:
    ip_function_map[ip]()
except KeyError:
    # Do default case if you can't find the IP address in dictionary
2 Likes