Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Words with Particular Rear letter

Improve Article
Save Article
  • Last Updated : 29 Dec, 2022
Improve Article
Save Article

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




# 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))

Output : 

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




# 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))

Output : 

The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat']
The list of matching last letter : ['Manjeet', 'akshat']

Method #3: Using find() method

Python3




# Python3 code to demonstrate
# Words with Particular Rear letter
 
# initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# initializing check letter
check = 'T'
 
# printing original list
print("The original list : " + str(test_list))
 
res=[]
# Words with Particular Rear letter
for i in test_list:
    if(i.find(check.lower())==len(i)-len(check)):
        res.append(i)
     
# print result
print("The list of matching last letter : " + str(res))

Output

The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat']
The list of matching last letter : ['Manjeet', 'akshat']

Method #4: Using filter()+lower()+lambda functions

Python3




# Python3 code to demonstrate
# Words with Particular Rear letter
 
# initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# initializing check letter
check = 'T'
 
# printing original list
print("The original list : " + str(test_list))
 
res = list(filter(lambda x: x[-1].lower() == check.lower(),test_list))
 
# print result
print("The list of matching last letter : " + str(res))

Output

The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat']
The list of matching last letter : ['Manjeet', 'akshat']

Time Complexity: O(N)

Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!