frcamacho
frcamacho

Reputation: 180

Accessing keys in dictionaries

If I wanted to access all the keys in a dictionary and check if the sequences like (ATGC) which is the key have "GC". How could I access the keys to check if it contains the string "GC"?

Many Thanks!

Upvotes: 1

Views: 151

Answers (1)

Jonathan Vanasco
Jonathan Vanasco

Reputation: 15680

matches = [ k for k in yourdictionary.keys() if 'gc' in k.lower() ]
number_of_matches = len(matches)

you could also do a regex or string.find ( returns -1 if not found ) or string.count -- but if substring in string works and is cleaner to illustrate the point. i also cast the key to lowercase and compared against a lowercase string, so it would effectively do a case insensitive match.

Upvotes: 3

Related Questions