Python | Find the list elements starting with specific letter
Last Updated :
09 May, 2023
Sometimes, we require to get the words that start with a specific letter. This kind of use case is quite common in 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
test_list = [ 'Akash' , 'Nikhil' , 'Manjeet' , 'akshat' ]
check = 'A'
print ( "The original list : " + str (test_list))
res = [idx for idx in test_list if idx[ 0 ].lower() = = check.lower()]
print ( "The list of matching first letter : " + str (res))
|
Output
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat']
The list of matching first letter : ['Akash', 'akshat']
Time complexity: O(n), where n is the length of the test_list. The list comprehension + lower() takes O(n) time
Auxiliary Space: O(n), where n is the number of elements in the list test_list
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
test_list = [ 'Akash' , 'Nikhil' , 'Manjeet' , 'akshat' ]
check = 'A'
print ( "The original list : " + str (test_list))
res = [idx for idx in test_list if idx.lower().startswith(check.lower())]
print ( "The list of matching first letter : " + str (res))
|
Output
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat']
The list of matching first letter : ['Akash', 'akshat']
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), where n is the number of elements in list res.
Method #3 : Using find() and lower() methods
Python3
test_list = [ 'Akash' , 'Nikhil' , 'Manjeet' , 'akshat' ]
check = 'A'
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
if (i.find(check) = = 0 or i.find(check.lower()) = = 0 ):
res.append(i)
print ( "The list of matching first letter : " + str (res))
|
Output
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat']
The list of matching first letter : ['Akash', 'akshat']
Method #4 : Using filter(),lambda function and lower() methods
Python3
test_list = [ 'Akash' , 'Nikhil' , 'Manjeet' , 'akshat' ]
check = 'A'
print ( "The original list : " + str (test_list))
res = list ( filter ( lambda x: x[ 0 ].lower() = = check.lower(), test_list))
print ( "The list of matching first letter : " + str (res))
|
Output
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat']
The list of matching first letter : ['Akash', 'akshat']
Method #5: Using re (regular expression)
The re (regular expression) approach to finding elements in a list that start with a specific letter involves using the re module in Python. This module allows you to use a special syntax called regular expressions to search for patterns in strings.
To use the re approach, you will first need to import the re module. Then, you can use the re.match() function to search for elements in the list that match a specific pattern. In this case, the pattern will be a regular expression that specifies that the element must start with the target letter.
Python3
import re
test_list = [ 'Akash' , 'Nikhil' , 'Manjeet' , 'akshat' ]
check = 'A'
check = check.lower()
print ( "The original list : " , test_list)
res = [x for x in test_list if re.match( "^{}" . format (check), x.lower())]
print ( "The list of elements that start with '{}': {}" . format (check, res))
|
Output
The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat']
The list of elements that start with 'a': ['Akash', 'akshat']
Time complexity: O(n)
Auxiliary space: O(n)
Method #6: Using numpy.array() and numpy.char.startswith()
This method uses NumPy library to create an array of strings from the given list and then applies numpy.char.startswith() to find the elements starting with a specific letter.
Python3
import numpy as np
test_list = [ 'Akash' , 'Nikhil' , 'Manjeet' , 'akshat' ]
check = 'A'
print ( "The original list : " + str (test_list))
np_arr = np.array(test_list)
res = list (np_arr[np.char.startswith(np_arr, check)])
res.extend( list (np_arr[np.char.startswith(np_arr, check.lower())]))
print ( "The list of matching first letter : " + str (res))
|
Output
The original list : [‘Akash’, ‘Nikhil’, ‘Manjeet’, ‘akshat’]
The list of matching first letter : [‘Akash’, ‘akshat’]
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), where n is the length of the test_list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...