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

Question:

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.

Asked By: Agota

||

Answers:

{key: val for key, val in dictionaryOne.items() if "aa" in val}

The best way to filter a dictionary in Python

Answered By: arrmansa

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)
Answered By: Kulasangar
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.