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

Anthony Lopez
Anthony Lopez
963 Points

Stuck on a question in Introducing Lists!

I'd like you to now only print continents that begin with the letter "A".

HINT: Remember that you can access characters in a string by index

continents = ['Asia,' etc.]

How do I call only the continents in that list that start with the letter A?

I'm not sure how to write it out:

for continent in continents: ????

continents.py
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
# Your code here
for continent in continents:
    print("A")

3 Answers

Hi Anthony,

For this challenge, it's important that you know how to access characters in a string by index. The first character of a string is at index position 0.

For example:

my_string = "Asia"
print(my_string[0])  # this gives me back the letter A

Now you could use this to check if a continent starts with the 'A', and if so, print it out:

for continent in continents:
    if continent[0] == "A":
        print("* " + continent)
Anthony Lopez
Anthony Lopez
963 Points

You rock Joey!

Thanks for the quick response. This makes sense but I still have some questions about this.

In the my_string example the 0 position of the string Asia is A. I understand that well.

But within the continents list specifically isn't the 0 postion the string "Asia", not the character A in the string Asia?

For example if I were to execute continents[0] it would print Asia not A.

I appreciate the help! I did not think to use the if statement at all. I have to remember to utilize everything I've learned so far!

Correct! If you asked for index position 0 on the continents list, you'll get back the first item from that list. In this case that's indeed 'Asia.'

But that's not what we asked in this for loop! Maybe it's a little bit more clear if we write it like this:

for item in continents:  # loop over each item in the continents list
    if item[0] == "A":
        print("* " + item)

Item stands for each individual item in the list (all the continent names). So item[0] gives us back the first character of that item. Just like my_string[0] did in the example above.

Hopefully I was able to answer your question!

Anthony Lopez
Anthony Lopez
963 Points

Thanks again! Yes that makes sense to me now.

Appreciate you taking the time Joey.

Arihant Tripathy
Arihant Tripathy
8,764 Points

I had the same question too! Thanks!