Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading