result = []
names = ['link', 'zelda', 'saria', 'ganon', 'volvagia']
lines = iter(data)
for line in lines:
while line.startswith(">") and any(name in line for name in names):
name = line
upper_seq = []
for line in lines:
if not line.isupper():
break
upper_seq.append(line)
else:
line = "" # guard against infinite loop at EOF
result.append((name, ''.join(upper_seq)))
If there are many names then set()
might be faster to find names in line instead of any(...)
:
names = set(names)
# ...
if line.startswith(">") and names.intersection(line[1:].split()):
# ...
Result
[('>link is the first', 'OIGFHFHAGIUUIIUFG'),
('>ganon is the fourth', 'ADGGHHHHHH'),
('>volvagia is the last', 'AFGDAAFGDAADFGAFDADFDFFDDFGAHUUERR')]