Python | Find the list elements starting with specific letter
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
# 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']
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
# 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']
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
# Python3 code to demonstrate # Words starting with specific letter # initializing list test_list = [ 'Akash' , 'Nikhil' , 'Manjeet' , 'akshat' ] # initializing check letter check = 'A' # printing original list print ( "The original list : " + str (test_list)) # Words starting with specific letter res = [] for i in test_list: if (i.find(check) = = 0 or i.find(check.lower()) = = 0 ): res.append(i) # 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 #4 : Using filter(),lambda function and lower() methods
Python3
# Python3 code to demonstrate # Words starting with specific letter # using filter + 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 filter + lambda function + lower() # Words starting with specific letter res = list ( filter ( lambda x: x[ 0 ].lower() = = check.lower(), test_list)) # 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 #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
# Python3 code to find elements in a # list that start with a specific letter import re # Initialize the list of elements test_list = [ 'Akash' , 'Nikhil' , 'Manjeet' , 'akshat' ] # Initialize the target letter check = 'A' check = check.lower() # Print the original list print ( "The original list : " , test_list) # Use a regular expression to match elements that start with the target letter res = [x for x in test_list if re.match( "^{}" . format (check), x.lower())] # Print the result print ( "The list of elements that start with '{}': {}" . format (check, res)) |
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
#Python3 code to demonstrate #Words starting with specific letter #using numpy.array() and numpy.char.startswith() import numpy as np #initializing list test_list = [ 'Akash' , 'Nikhil' , 'Manjeet' , 'akshat' ] #initializing check letter check = 'A' #printing original list print ( "The original list : " + str (test_list)) #create a numpy array from the list np_arr = np.array(test_list) #using numpy.char.startswith() #Words starting with specific letter res = list (np_arr[np.char.startswith(np_arr, check)]) res.extend( list (np_arr[np.char.startswith(np_arr, check.lower())])) #print result 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.
Please Login to comment...