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

Sanjay Devadkar
PLUS
Sanjay Devadkar
Courses Plus Student 1,016 Points

I'd like you to now only print continents that begin with the letter "A". HINT: Remember that you can access characters

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.py
continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']
# Your code here
for continent in continents:
    print("* " + (continent))
    print(A)

3 Answers

Alissa Kuzina
Alissa Kuzina
5,835 Points

Hey! This is how i did it

continents = ['Asia', 'South America', 'North America', 'Africa', 'Europe', 'Antarctica', 'Australia']

for continent in continents: 
    if continent[0] == 'A':
        print("* " + continent)

The thing is that you need continents where first letter is 'A'. So we can iterate throug each continent and access first letter in it, In every iteration we have continent, If my condition is true I print this word on the screen .

Sanjay Devadkar
Sanjay Devadkar
Courses Plus Student 1,016 Points

Can you explain the code, As per my understanding if you see your code line## if continenet[0] == 'A': statement## it should only give Asia as it is at zero position right.

Alissa Kuzina
Alissa Kuzina
5,835 Points

No, you receive Asia , Africa, Antarctica, Australia and you can see it if you run this code . The thing is that with for loop I "touch" every "continent" in "continents". With continent[0] I access the first letter of each "continent" ( in Asia - A, in South America - S, in North America - N and so on). Then with comparison opeator I check what is A. Then I print the name of continent where results were equal (A=A).

Ralph Wright
Ralph Wright
5,869 Points

Thank you so much for this! This is very clever actually. Great Job!

Elgene Ee
Elgene Ee
3,554 Points

Hi Sanjay, try this out, using 2 'for loops' seems kinda impractical and long codes. However it works, take some time to dissect what I am doing, you can do it!

my_list = []
char = 'A'
for item in continents:
  if item[0] in char:
    my_list.append(item)
  else:
    continue
for continent in my_list:
  print('* ' + continent)
Elgene Ee
Elgene Ee
3,554 Points

Yes ofc there are other ways to solve this problem, think programming more like a mathematical question, it is about solving problems, not memorizing to solve. If you ask me the best way, there are certainly no best way to solve, everyone has their own style, make sure you grasp the basics fundamentals of Python, and make yourself alot of python program until you get the hang of it...You can do it!

my_list = [] char = 'A' for item in continents: if item[0] in char: my_list.append(item) else: continue for continent in my_list: print('* ' + continent) It's still gives me a bummer

if continent[0] == 'A': print("* {}".format(continent))