Python | Find the list elements starting with specific letter
Sometimes, we require to get the words that start with the specific letter. This kind of use case is quiet common in the places of common programming projects or competitive programming. Let’s discuss certain shorthands to deal with this problem in Python.
Method #1 : Using list comprehension + lower()
This problem can be solved using the combination of above two functions, list comprehension performs the task of extending the logic to whole list and lower function checks for case insensitivity with the target word of argument letter.
# Python3 code to demonstrate # Words starting with specific letter # using list comprehension + lower() # initializing list test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat'] # initializing check letter check = 'A' # printing original list print("The original list : " + str(test_list)) # using list comprehension + lower() # Words starting with specific letter res = [idx for idx in test_list if idx[0].lower() == check.lower()] # print result print("The list of matching first letter : " + str(res)) |
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat'] The list of matching first letter : ['Akash', 'akshat']
Method #2 : Using list comprehension + startswith() + lower()
This method is similar to the above method but rather than checking for equality with operator, it checks using the startswith function which is inbuilt provided by python inbuilt library.
# Python3 code to demonstrate # Words starting with specific letter # using list comprehension + startswith() + lower() # initializing list test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat'] # initializing check letter check = 'A' # printing original list print("The original list : " + str(test_list)) # using list comprehension + startswith() + lower() # Words starting with specific letter res = [idx for idx in test_list if idx.lower().startswith(check.lower())] # print result print("The list of matching first letter : " + str(res)) |
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat'] The list of matching first letter : ['Akash', 'akshat']
Recommended Posts:
- Python | Checking if starting digits are similar in list
- Python | Remove element from given list containing specific digits
- Python | Sort list of tuples by specific ordering
- Python | Index specific cyclic iteration in list
- Python | Find sum of frequency of given elements in the list
- Python program to find sum of elements in list
- Python | Find all elements count in list
- Python | Find missing elements in List
- Python | Find elements of a list by indices
- Python program to find N largest elements from a list
- Python | Find common elements in list of lists
- Python | Find top K frequent elements from a list of tuples
- Python | Find longest consecutive letter and digit substring
- Python regex to find sequences of one upper case letter followed by lower case letters
- Python | Replace elements in second list with index of same element in first list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



