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 Introducing Lists Using Lists Continental

Thierry van Wessel
Thierry van Wessel
2,141 Points

I just have no idea how to select an list item on condition that it starts with the letter A.

I have a list of 7 continents and should only print out the continents that start with letter A.

continents.py
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
# Your code here

for continent in continents:
    print("* " + continent.index)

1 Answer

boi
boi
14,241 Points

I want you to figure this out by yourself so DON'T look at the direct solution below BEFORE reading the indirect solution.

INDIRECT SOLUTION: suppose you have a list of words, and in those list of words I want you to give me words that start with an alphabet "A", so your solution would be something like;

For each word in this list of words, if the alphabet "A" is the first letter in this word, I want that word

DIRECT SOLUTION:

for word in words:
    if "A" == word[0]:
        print(word)

The actual solution to the challenge:

since the instructor wants you to make use of indexing, let's solve using indexing

for continent in continents:

    if "A" == continent[0]:   # Made use of indexing here uisng the "if" condition

        print("* " + continent)

That's it

I wasn't the original person asking this question, but I was also stumped. I just wanted to let you know that you helped me to understand! I was missing the double "==". I only had "=".

boi
boi
14,241 Points

Glad it helped you Elisha.