user2300369
user2300369

Reputation: 308

Checking presence of all elements of one iterable in the other without using loop in python

Let's say I have a list amino=['A', 'G', 'L'] and a string sequence='GIIKAKILMDAAALG'. Now, if I knew how to use loop I would do something like this to check whether one of the elements in my amino list is present in my sequence:

for el in amino:
  if el in sequence:
    print 'Amino acid %s is found in sequence'% el
    break

Nice, but let's assume I do not know loop. Can I do something like above using some attributes of lists and strings?

Upvotes: 0

Views: 35

Answers (1)

Jakub Wasilewski
Jakub Wasilewski

Reputation: 2976

You can turn your sequences into sets, which lets you use simple set operations to get the answer:

set(amino).isdisjoint(set(sequence))   # True if the sets have nothing in common
set(amino).intersection(set(sequence)) # a set of common elements

No visible loops, though obviously the set operations will likely use them underneath the hood.

Upvotes: 2

Related Questions