PYTHON Remove repetitive chars from string -
this question has answer here:
- remove items list while iterating 18 answers
- remove specific characters string in python 18 answers
i have string name = "aaron" , remove vowels. using remove() if char repetive letter 'a' in case stays in string. suggestions ? here code :
def disemvowel(word): word_as_list = list(word.lower()) print(word_as_list) vowels = ["a", "e", "i", "o", "u"] char in word_as_list: v in vowels : if char == v : word_as_list.remove(v) return "".join(word_as_list) print(disemvowel("aaaron"))
as other people have said, list comprehension way go:
def disemvowel(word): word = word.lower() vowels = ["a" , "e", "i", "o", "u"] return "".join([char char in word if char not in vowels]) print disemvowel("aaaallbag")
Comments
Post a Comment