Python Forum

Full Version: replace vowels
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello, i have a list with some names, and i have to replace wovels by " - "
here is what i tried:



list1 = ['john', 'tony', 'luck]


def replace_vowel(mlist):
            vowels = "aeiouy"
            novowel = ""
            for char in mlist:
                if char in vowels:
                    novowel = novowel.replace(char, '-')
                    return mlist

print replace_vowel(list1)
actually , the output is " None "
It has to be:

Output:
['j-hn', 't-n-', 'l-ck']
any idea? thanks !
You are looping over list1, which is 'john', 'tony', and 'luck'. None of those are in vowels, so your if statement never triggers.

You need two loops. First loop over the words in mlist, which effectively what you are doing now. Then loop over the characters in vowels. Then replace that character with a hypen (-, not an underscore _) in the word. You will need to build a list of the modified words and return that after both loops are finished.
thanks for your answer,
i now tried that:



list1 = ['john', 'tony', 'luck]

def replace_vowel(mlist):
    vowels = "aeiouy"
    new_word = []
    for letter in mlist:
        if letter in vowels:
            new_word.append('-')
    else:
        new_word.append(letter)
    return new_word

print replace_vowel(list1)
but this just prints the list:

Output:
['john', 'tony', 'luck]
You create a new list (named new_word), but then return whatever you were originally passed.  So it doesn't matter what transformations you do, as they all get ignored anyway.

But even if you did return the new list, you still wouldn't see any transforms, since you iterate over words, never letters.
That is not really what I suggested. I suggested two loops, one of the words in the list, one over the letters in vowels. I also suggested using replace to replace the vowels, and only appending to new_list after the loop over vowels is done (actually, I may have misstated that last part, but that's what it should be). Look at these lines:

for letter in mlist:
    if letter in vowels:
mlist is a list of words (names, really). So why would you call the loop variable 'letter'? And why would a whole name be in vowels?
is there a way to do it like this ? since i want EVERY vowels to be replaced by '-'

list1 = ['john', 'tony', 'luck', 'o']
 
def replace_vowel(mlist):
    vowels = 'aeiouAEIOU'
    newlist = [c.replace(vowels, '-') for c in mlist]
    return newlist

 
print replace_vowel(list1)
Output:
['john', 'tony', 'luck', '-']
or with:
     newlist = ['-' if c in 'aeiou' else c for c in mlist]
     return newlist
same output
list1 = ['john', 'tony', 'luck', 'o', 'mIssissippi']
  
def replace_vowel(mlist):
    vowels = 'aeiou'
    newlist = []
    for word in mlist:
        for char in word:
            if char.lower() in vowels:
                word = word.replace(char, '-')
        newlist.append(word)
    #newlist = [c.replace(vowels, '-') for c in mlist]
    return newlist
 
print(replace_vowel(list1))
Output:
['j-hn', 't-ny', 'l-ck', '-', 'm-ss-ss-pp-']
thanks!
or
def replace_vowel(word):
    vowels = 'aeiouAEIOU'
    for vowel in vowels:
        word = word.replace(vowel,'-')
    return word

list1 = ['john', 'tony', 'luck', 'o', 'mIssissippi']
print(map(replace_vowel, list1))
Output:
['j-hn', 't-ny', 'l-ck', '-', 'm-ss-ss-pp-']
Puny mortals, and iterating over characters in a word... (ie, if we're solving homework, at least give an answer that looks like they didn't do it themselves ;)
>>> def replace_vowel(words):
...   replacements = str.maketrans({ch:"-" for ch in "aeiouAEIOU"})
...   for word in words:
...     yield word.translate(replacements)
...
>>> items = ['john', 'tony', 'luck', 'o', 'mIssissippi']
>>> list(replace_vowel(items))
['j-hn', 't-ny', 'l-ck', '-', 'm-ss-ss-pp-']