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

Searching a prefix in list of dictionaries in python

I have currently a list of dictionaries

list_of_dicts = [{"name": 'stfdahbancssc',"status":'running'},
                 {"name": 'stfdahbancssc',"status":'running'}]

I want to check how can i match the prefix 'stf' and print stf exists otherwise stf does not exist .

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

>Solution :

Iterate the dictionaries, get the "name" value, check for prefix, print message:

list_of_dicts = [{"name": 'stfdahbancssc',"status":'running'},
                {"name": 'stfdahbancssc',"status":'running'},
                {"name": 'none'}]

for idx,d in enumerate(list_of_dicts): 
    if d.get("name","").startswith("stf"):
        v = d["name"]
        s = d["status"]
        print(f"The {idx} dictionary has {v} as {s}")
        print("a")
    else:
        print(f"The {idx}. dictionary has no name starting with 'stf'.")    

Output:

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