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 Python Basics (2015) Shopping List App Continue

How to check each item in a list to see if it begins with the letter 'a'.

I know I need to do some kind of loop. I have tried looking in the help menu of python all about indexes, however I cant get my code to work properly. I feel like I might be over thinking it a little.

Am I wrong in assuming this: list = ['123', '456', '789'] 123 has an index of 0 456 has an index of 1 and 789 has an index of 2

If that is the case then I am not sure on how to check if a certain index of a list starts with something specific. In this case i need to check to see if it starts with an "a".

If you could either point me to a video that helps explain this, or give me a hint on which function I should be using, I would be most grateful.

breaks.py
def loopy(items):
    # Code goes here
    for item in loopy:
        if item.index(0) == "a":
            continue
        else:
            print(loopy)

1 Answer

Stuart Wright
Stuart Wright
41,118 Points

There are 3 things you need to fix in your code:

    for item in loopy:

loopy is the name of the function. items is the list variable you should be looping over.

item.index(0)

This doesn't find the list item at index 0. It looks for the index for an element with value 0. To access the item at index 0 in your item you simply use item[0].

print(loopy)

Again, loopy is the function name, which is not what you want to print out. You want to print the current item.

Hopefully this is enough to get you on the right track. Let me know if you need any more help.

Thank you so much! I was able to complete it with your help. It seems like a silly mistake, however I understand it better. Thanks again!