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??
Asked
Active
Viewed 2,439 times
-4
-
Combine the duplicate I linked to with `startswith('A')` – DeepSpace Aug 22 '16 at 08:28
-
3So what have you tried so far? – cdarke Aug 22 '16 at 08:28
-
Iam new to programming as well as python. I just started to learn programming in stack overflow.. Could you help me out from the above question I asked? – Fayaz Aug 22 '16 at 08:35
1 Answers
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