Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
i'm really stuck in this challenge
create a variable named y_words that uses a list comprehension to only contain the words from words that start with the letter "y".
here is my code
words = [ 'yellow', 'red', 'yesterday', 'tomorrow', 'zucchini', 'eggplant', 'year', 'month', 'yell', 'yonder', ] y_words = [ y for y in words]
Kristian Gausel14,661 Points
y_words = [ y for y in words if y == "y"]
The code is pretty self-explanatory. You just missed the condition to be included =)
Chris Grazioli31,225 Points
I don't like it when people use vague names for things, also it sucks for the person new to the challenge trying to understand what is going on TRY THIS
words = [ 'yellow', 'red', 'yesterday', 'tomorrow', 'maybe', 'zucchini', 'eggplant', 'year', 'month', 'yell', 'yonder', 'today', ] #This might make more sense to some y_words= [word for word in words if word=='y']
Explained left to right: Its essentially saying "y_words" is a list comprehension where it appends the current "word" being considered from "word" in "words" (Which is just another way of saying the nth item in the list ) "if" the first letter in the word currently being considered is a letter y "word=='y' "
Explain another way: You can break the Comprehension into a few parts:
=[what it will add to the list + for + a thing + in + a list of things + if some condition the thing meets]
=[word + for + word + in+ words + word=='y' ]