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 Regular Expressions in Python Introduction to Regular Expressions Word Length

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

word_length.py: Still Getting Bummer

Im getting this :

Didn't get the right output. Output was [['123456'], ['Treehouse'], ['student'], ['Kenneth'], ['Python'], ['g0tcha']]. Length was 6.

When i test the code into my IDE it works or it seems to be work for me.

I'll try from to many ways but i don't know what's wrong :S

word_length.py
import re

# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
def find_words(count, data):

    # first i need to split the args
    # then i need to store in to the list 
    # and make a loop to apply the regex, if it matches stores in a new list
    # done
    item_list = []
    item_list = data.split(", ")
    times = count * "\w"
    regex = times + "+"
    print(regex)
    return_list = []
    # print(times)

    for item in item_list:
        if re.findall(regex, item):
            # print(re.match(r'\w\w\w\w+', item))
            # print(times + "+" + item)
            print(re.findall(regex, item))
            return_list.append(re.findall(regex, item))

    return return_list


# find_words(4, "dog, cat, baby, balloon, me")

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Do you want to append to the list or extend it?

Let me know if that hint doesn't do the trick ;)

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

oh i see!! , the append it's only for objects and extend is for the list.

Thanks a lot! Craig Dennis i was a little stuck with this.

Hey Aldo, My initial solution was similar to yours and it also didn't work (via the challenge and via my idle). Eventually I discovered that the output of variable regex was not what I expected:

regex = "\w" + "{" + str(4) + ",}"
>>> regex
'\\w{4,}'

This meant that when we're using it in re.findall() it actually looks like this:

re.findall('\\w{4,}', item)

The answering post in https://teamtreehouse.com/community/wordlength shows a solution to this challenge, which I found helpful.

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

Oh i see, hum, so is i the regex pattern format the problem , well i need to study this, because this was make me nutts :P

Thank you !!

I think you are over complicating this challenge. Once you have split the string into a list of strings (which you did), all you need to do is check the length of each string and add it to a new list if length is greater than the count.

Aldo Rivadeneira
Aldo Rivadeneira
3,241 Points

ok i understand what you mean, ill try the following but im still getting the same error of the length, well in fact im doing the regex not counting the legth of the word.

The strange thing is in my IDE this works...

BTW, Thanks for your time!

import re

# EXAMPLE:
# >>> find_words(4, "dog, cat, baby, balloon, me")
# ['baby', 'balloon']
def find_words(count, data):

    # first i need to split the args
    # then i need to store in to the list 
    # and make a loop to apply the regex, if it matches stores in a new list
    # done
    item_list = []
    item_list = data.split(", ")
    regex = "\w" + "{" + str(count) + ",}"
    return_list = []

    for item in item_list:
        if re.findall(regex, item):
            print(item)
            # print(re.findall(regex, item))
            return_list.append(re.findall(regex, item))

    return return_list


find_words(4, "dog, cat, baby, balloon, me")