How to print out the elements in a list that begin with a certain letter

Viewed 124
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
for continent in continents:
    print(continent)

if continent[0] == 'A':
    print(continent)

This is my code that I have used. Could someone see if there is a way to print out the continents that only begin with the letter "A"?

8 Answers

Use startswith()

continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
for continent in continents:
    if continent.startswith('A'):
        print(continent)

There are many ways to do this:

  1. Use a simple list comprehension:
[continent for continent in continents if continent.startswith('A')]
  1. Use a very similar generator expression:
(continent for continent in continents if continent.startswith('A'))
  1. Use a simple filter expression:
filter(lambda x: x.startswith('A'), continents)

2 and 3 is light on on memory - you can use it if you have a data-set that is really huge. 3 is a functional way of writing the same thing.

just indent the if in order for it being inside the loop. (because we want the for loop to go through the list and check for every element if it starts with 'A')

continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
for continent in continents:
    if continent[0] == 'A':
      print(continent)
  • Try this one
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
for continent in continents:
    if continent[0] == 'A':
        print(continent)

  • output:
Asia
Africa
Antarctica
Australia

Do it like that:

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

a_continents = [c for c in continents if c.startswith('A')]
# Or:
# a_continents = [c for c in continents if c and c[0] == 'A']

print(a_continents)

Code using while loop with elapsed time(Time taken by code to execute task)

from datetime import datetime

continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
i = 0
continent = sorted(continents)
start_time = datetime.now().time().microsecond
while i < len(continent):
    if (continent[i][0] == 'A'):
        print(continent[i])
        i = i + 1
    else:
        break
end_time = datetime.now().time().microsecond
print('Time taken :', end_time - start_time, 'ms')

OutPut:-

Africa
Antarctica
Asia
Australia
Time taken : 45 ms
continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
aNamedContinents = c
    for c in continents:
        if c.startswith('A'):
            print(c)

Use an if statement inside the for loop:

continents = [
    'Asia',
    'South America',
    'North America',
    'Africa',
    'Europe',
    'Antarctica',
    'Australia',
]
for continent in continents:
    if continent[0] == 'A':
        print(continent)
Related