List comprehensions

all() Returns true if all of the items in list are True

>>> print(all([True, True, True, True]))
True
>>> print(all([False, True, True, False]))
False

In above problem we need to check if all vowels are present in a string (ex: businesswoman) using all as follows:

>>> all(t in "businesswoman" for t in 'aeiouu')
True

Similarly we need to do it for all items in the W as follows:

>>> W = ['cat', 'audiotape', 'businesswoman', 'dog'] 
>>> [x for x in W if all(t in x for t in 'aeiouu')]
['audiotape', 'businesswoman']
>>> sorted([x for x in W if all(t in x for t in 'aeiouu')], key=len)[-1]
'businesswoman'

Read more about all()

Leave a Comment