Python Introducing Lists Using Lists Continental

Ryan Aves
Ryan Aves
2,203 Points

Hi all, Do you know why this version of code won't work for showing the continents beginning with "A"?

I tried creating a new list that only pulls "A" continents, using indexed values from the original continents list. That's still returning errors for some reason.

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


continents = continents[0,3,5,6]

print("Continents:")
for continent in continents:
  print("* " + continent) 

1 Answer

Steven Parker
Steven Parker
182,438 Points

You can't select multiple values from a list that way. But you don't want to modify the list anyway.

Just use an "if" statement so that the "print" only runs when the item begins with "A". The conditional expression can isolate the first letter of a word by using indexing with a value of 0.

Ryan Aves
Ryan Aves
2,203 Points

Gotcha, would this work as the "if" statement?

if continents[0] = "A":

print("Continents:") for continent in continents: print("* " + continent)

Steven Parker
Steven Parker
182,438 Points

Almost. A single "=" is an assignment operator. The comparison operator is "==".

Also, the "if" statement will need to be placed between the "for" and the "print" lines.