Open In App

Python – Words with Particular Rear letter

Last Updated : 28 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 the 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))
 
# Words with Particular Rear letter
# using list comprehension + lower()
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']

Time Complexity: O(n), where n is the length of the input list. This is because we’re using the list comprehension + lower() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

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']

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

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=[]
 
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))
 
# Reading corresponding letter in string
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)

Approach 5: Using Regex

This approach uses the python “re” module to search for the desired pattern in each word in the list. The pattern is constructed using the check letter and the “$” symbol, which matches the end of the string. The search function returns a match object if a match is found, and we use a list comprehension to extract the matching words.

Python3




import re
 
#initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
#initializing check letter
check = 'T'
 
#printing original list
print("The original list : " + str(test_list))
 
#using regex
result = [word for word in test_list if re.search(check.lower() + "$", word.lower())]
 
#print result
print("The list of matching last letter : " + str(result))
#This code is contributed by Edula Vinay Kumar Reddy


Output

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

Time complexity: O(n) for constructing the regex pattern and O(m) for each search, where m is the length of the word.
Auxiliary Space: O(1)

Method 6: Using a for loop and string slicing. 

Approach:

  1. Initialize an empty list called result to store the matching words.
  2. Loop through each word in the test_list.
  3. Check if the last letter of the word (using slicing [-1]) is equal to the lowercase check letter.
    • If it is a match, append the word to the result list.
  4. After the loop, print the result list.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# Words with Particular Rear letter
# using for loop and string slicing
 
# Initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# initializing check letter
check = 'T'
 
# printing original list
print("The original list : " + str(test_list))
 
# using for loop and string slicing
# Words with Particular Rear letter
result = []
 
for word in test_list:
    if word[-1].lower() == check.lower():
        result.append(word)
 
# print result
print("The list of matching last letter : " + str(result))


Output

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

Time complexity: O(n), where n is the length of test_list.
Auxiliary space: O(m), where m is the number of words in test_list that have the particular rear letter.

Method #7: Using map() + filter() + lambda functions

we can use map() function to apply lower() function on each element of the list, and then use filter() function along with lambda function to filter the elements having the last character same as the check character.

Python3




# initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# initializing check letter
check = 'T'
 
# printing original list
print("The original list : " + str(test_list))
 
# using map() + filter() + lambda functions
# Words with Particular Rear letter
res = list(filter(lambda x: x[-1].lower() ==
                  check.lower(), map(str.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), where n is the length of the input list.
Auxiliary space: O(n), as we are storing the filtered elements in a separate list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads