-4

I want to print an elements in an list contains more than 100 names.. In those 100 names I want to print only the names which have the starting letter 'A'.. How can I do this in python??

vaultah
  • 44,105
  • 12
  • 114
  • 143
Fayaz
  • 1
  • 2

1 Answers1

0

You may use list comprehension:

>>> my_list = ['apple', 'Alexander', 'Ball', 'Alphabet']
>>> a_list = [element for element in my_list if element.startswith('A')]
>>> a_list
['Alexander', 'Alphabet']

Alternatively, more Pythonic way to achieve this is by using itertools.ifilter():

>>> import itertools
>>> my_list = ['apple', 'Alexander', 'Ball', 'Alphabet']
>>> list(itertools.ifilter(lambda x: x.startswith('A'), my_list))
['Alexander', 'Alphabet']
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126