Welcome to the Treehouse Community
Looking to learn something new?
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.
def loopy(items): # Code goes here for item in loopy: if item.index(0) == "a": continue else: print(loopy)
Stuart Wright41,103 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.
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.
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.