Python Introducing Lists Using Lists Continental

Im Stuck on challenge Task 2 of 2...

I need to list every continent that begins with A but i am not sure how to do this.
continents = [
    'South America',
    'North America',

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

1 Answer

Hi Dharmesh,

To achive this you have multiple options

Use if

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

As you can index strings continent[0] is the first character of continent.

Use filter()

for continent in filter(lambda c: c[0] == "A", continents):
         print("* " + continent)

Both work fine, I tend to use the if in the loop (with filter() you are first filtering for all continents that start with 'a' and then loop over them), as it looks cleaner and is more performant;

Thank you Hakim.

Can you explain if my understanding is correct..

The if function is looking at the first letter in the variable continent to check if it is equal to A.

Correct, but there is no 'if function' if is a construct, a construct is on the language level (build into the interpreter), a function is on the higher library level (code you write or from standard import).