devowel vowels

This exercise is to find as many different ways of removing vowels from the word "vowels". Unless stated, all operations assume:

# create list of letters from the word vowels
vowels = list('vowels')

Manually remove letters with splice operator:

del vowels[1:4:2] # [start: stop: stride]

vowels
# output
# ['v', 'w', 'l', 's']

Function:

def devowel(letters, y=[]):
    """Remove vowels from a list of letters."""
    vowels = list('aeiou')
    for letter in letters:
        if letter not in vowels:
            y.append(letter)
    return y

devowel(vowels)
# Output
# ['v', 'w', 'l', 's']

List comprehension:

[x for x in testing if x not in list('aeiou')]

# Output
# ['v', 'w', 'l', 's']

Lamda function:

filter(lambda x, v=list('aeiou'): x not in v, vowels)

# Output
# ['v', 'w', 'l', 's']

Generator with yield:

def devowel(object):
    for x in object: 
        if x not in list('aeiou'):
            yield x

for i in devowel(vowels): print i

# Output
# v
# w
# l
# s

Generator expression:

# Not using lists od letters
genexp = (x for x in 'vowels' if x not in 'aeiou')
print genexp.next()
# V
print genexp.next()
# w
# ...

results matching ""

    No results matching ""