Search code examples
pythondictionaryfindkey

How to find if in created dictionary the chosen sequence has two aa letters?


Lets say I have a dictionary. Is it possible to use the input function to enter the key and get the True for its specific value:

dictionaryOne = { "ABCD" : "aatttgggtcatcg", "SSEE" : "atgcccgta", "GTAD" : "atgccggaaa" }

I tried using if and else including find function but I am very new in this and don't know how to combine everything.


Solution

  • You could do something like this to determine whether your value has aa. I'm assuming you only to need to print the bool value

    is_exist = False
    for key, val in dictionaryOne.items():
        if 'aa' in val:
            is_exist = True
        print(f'{key}: {is_exist}')
    

    UPDATE:

    # Gets user input
    name = input("enter the key: ")
    
    dictionaryOne = { "ABCD" : "aatttgggtcatcg", "SSEE" : "atgcccgta", "GTAD" : "atgccggaaa" }
    
    try:
        val = True if 'aa' in dictionaryOne.get(name) else False    
        print(val)
    except Exception as k:
        print(k)