Sometimes, we require to get the words that end 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 with Particular Rear letter # using list comprehension + lower() # initializing list test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat'] # initializing check letter check = 'T' # printing original list print("The original list : " + str(test_list)) # using list comprehension + lower() # Words with Particular Rear letter res = [idx for idx in test_list if idx[len(idx) - 1].lower() == check.lower()] # print result print("The list of matching last letter : " + str(res)) |
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat'] The list of matching last letter : ['Manjeet', 'akshat']
Method #2 : Using list comprehension + endswith() + lower()
This method is similar to the above method but rather than checking for equality with operator, it checks using the endswith function which is inbuilt provided by python inbuilt library.
# Python3 code to demonstrate # Words with Particular Rear letter # using list comprehension + endswith() + lower() # initializing list test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat'] # initializing check letter check = 'T' # printing original list print("The original list : " + str(test_list)) # using list comprehension + endswith() + lower() # Words with Particular Rear letter res = [idx for idx in test_list if idx.lower().endswith(check.lower())] # print result print("The list of matching last letter : " + str(res)) |
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat'] The list of matching last letter : ['Manjeet', 'akshat']
Recommended Posts:
- Python - Eliminate Capital Letter Starting words from String
- Python | Front and rear range deletion in a list
- Python | Append at front and remove from rear
- Access front and rear element of Python tuple
- Python | Strings with similar front and rear character
- Python | Replace rear word in String
- Python - Remove rear element from list
- Python - Rear element extraction from list of tuples records
- Python - Test rear digit match in all list elements
- Python | Alternate front - rear Sum
- Python | Rear Addition of Record
- Python | Rear elements from Tuple Strings
- Python - Rear shift K in List
- Python | Alternate Rear iteration
- Python | Shift from Front to Rear in List
- Python | Retain K Front and Rear elements
- Python | Kth Non-None String from Rear
- Python - Remove Rear K characters from String List
- Python - Rear character String categorization
- Python | First character occurrence from rear String
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.

