Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Jared Barr
Jared Barr
1,911 Points

Disemvowel -> How better could I have pythonized my code?

Hello, This code worked but I know I could have made it cleaner. Anyone have any suggestions cutting down the lines / extraneous steps?

Appreciate it!

vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]


def disemvowel(word):   
    word_list = []
    for i in word:
        word_list.append(i)
    for letter in word.lower():
        if letter in vowels:
            try:
                word_list.remove(letter)
            except ValueError:
                pass

    word = "".join(word_list)
    return word


disemvowel("No VOwels Allowed")

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You code is very readable, and works so I'll not restructure the code, but only show shortcuts you could have used.

vowels = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
less_vowels = "aeiou"          # `in` works directly on strings!


def disemvowel(word):
    # word_list = []           # Can create word_list using
    # for i in word:           # list() function
    #     word_list.append(i)
    word_list = list(word)
    for letter in word:  # .lower():       # .lower() unnecessary if `vowels`
        #                                  # has both cases present
        # if letter in vowels:             # `vowels` could be lowercase only
        if letter.lower() in less_vowels:  #  -OR- `if letter.lower() in vowels:`
            #                              # used instead
            # try:                         # try not need if '.lower()` removed
            word_list.remove(letter)       # .remove() scans entire word_list
            # except ValueError:           # every loop and removes
            #     pass                     # In original code only lowered
            #                              # letters removed {bug!}

    word = "".join(word_list)
    return word

As for ways to avoid repeatedly scanning the word_list using remove() consider, starting with an empty word_list, then only append() to it if letter.lower() is not in vowels.

Post back if you wish more feedback. Good luck!

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Well, the important thing is to first understand the individual steps and take note of Chris' items.

I think one of spiffiest features of Python is list comprehensions, good course to take (didn't look up the link).

Here's how it can be a one liner:

def disemvowel(string):
    return ''.join([e for e in string if e.lower() not in 'aeiou'])
Jared Barr
Jared Barr
1,911 Points

i really like these but I don't quite get it. I can barely read it and I don't think I'd be able to build my own YET.