python iterate through list to check if string from variable exists

I am trying to write a small script that will check a hostname from a host and if the hostname has a specific string in the name from a list, then print the string.

Basically, the hostname has its type in the name so I want to check what type of host it is;

from os import uname
host = uname()[1]
if "consumer" in host:
    print("consumer")
elif "search" in host:
    print ("search")

But I would like to have it check from a list like;

host_type = ["control", "consumer", "search", "hub"]

rather than else and iterate through

>Solution :

You can use a loop:

for check_name in host_type:
    if check_name in host:
        print(check_name)
        # break  # use to stop loop after first hit

Leave a Reply