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 trialIdris Abdulwahab
Courses Plus Student 2,961 PointsContinue. skipping items in a list.
The quiz was to skip printing an item if that item starts with the character "a". And then proceed to print the next item instead.
def loopy(items):
# Code goes here
for item in items:
for i in range(0, len(item)):
if i == "a":
continue
print(items)
ilya A
3,802 PointsThis comment is for hamsternation. What does .lower mean? I cannot recall it from any of the lessons... Also why did you use zero between square brackets?
1 Answer
hamsternation
26,616 Pointsquestion: This comment is for hamsternation. What does .lower mean? I cannot recall it from any of the lessons... Also why did you use zero between square brackets?
All of this goes back to the fact that items
is a list of strings, so item
is basically a string.
I'm not sure if you've had the lesson on indexes yet (If not, I'm sure it's coming up soon), but objects that are iterable can be indexed using the bracket notation [].
example:
>>> list = ['hi', 'my', 'name', 'is', 'hamsteration']
>>> list[0]
'hi'
>>> list[1]
'my'
for an iterable, the index starts at 0 for the first item, and 1 for the 2nd, etc. (this might be a bit confusing at first, but you'll get used to it)
so for item, since it's a string, and iterable (which basically means it can be indexed), you can do the same thing with it.
item[0]
just means the first letter of the string.
any_string.lower()
is just one of the many methods for a string, in this case, returns a lowercased version of that string. Some similar methods include .upper (uppercased), .title (titlecased), etc. for a full list, type help(str)
in your python interpreter.
so putting it all together, item[0].lower()
simply returns the first letter of the string in lowercase back to the logical statement, which then compares it to the letter "a" in this case.
:) I hope this helps!
hamsternation
26,616 Pointshamsternation
26,616 PointsWhat you want to do here is to skip the entire
item
if the item name starts with 'a'.Your for loop currently pulls out an item at a time from your items list, which is perfect, but in you do not want to create another nested loop, which you have done, but to simply check if the first letter of
item
starts with the letter 'a', and if so continue, elseprint(item)
.without looking at the exact prompt, something I think might work would be: