Open In App

Python | Kth index character similar Strings

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, we require to get the words that have the Kth index with the specific letter. This kind of use case is quiet common in places of common programming projects or competitive programming. Let’s discuss certain shorthand 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
# Kth index character similar Strings
# using list comprehension + lower()
 
# Initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# Initializing check letter
check = 'k'
 
# initializing K
K = 2
 
# Printing original list
print("The original list : " + str(test_list))
 
# Kth index character similar Strings
# using list comprehension + lower()
res = [idx for idx in test_list if idx[K - 1].lower() == check.lower()]
 
# Printing the result
print("The list of matching Kth letter : " + str(res))


Output : 

The original list : ['Akash', 'Nikhil', 'Manjeet', 'akshat']
The list of matching Kth letter : ['Akash', '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 #2: Using a for loop

  • Initialize an empty list res to store the matching strings.
  • Loop through each string in the test_list.
    • Use the lower() method to convert the current string to lowercase.
    • Use an if statement to check if the K-th character of the current string (also converted to lowercase) matches the check character (also converted to lowercase).
    • If the condition is true, append the current string to the res list.
  • Print the res list.

Python3




# Python3 code to demonstrate
# Kth index character similar Strings
# using for loop
 
# initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# initializing check letter
check = 'k'
 
# initializing K
K = 2
 
# printing original list
print("The original list : " + str(test_list))
 
# using for loop
# Kth index character similar Strings
res = []
for string in test_list:
    string_lower = string.lower()
    if string_lower[K-1] == check.lower():
        res.append(string)
 
# print result
print("The list of matching Kth letter : " + str(res))


Output

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

Time complexity: O(n*m), where n is the length of test_list and m is the length of the longest string in test_list. The for loop runs once for each string in test_list, and the lower() method has a time complexity of O(m)
Auxiliary space: O(k), where k is the number of strings in test_list that match the condition. 

Method 3:  using filter() function and lambda function:

Python3




# Python3 code to demonstrate
# Kth index character similar Strings
# using filter() + lambda function
 
# initializing list
test_list = ['Akash', 'Nikhil', 'Manjeet', 'akshat']
 
# initializing check letter
check = 'k'
 
# initializing K
K = 2
 
# printing original list
print("The original list : " + str(test_list))
 
# using filter() + lambda function
# Kth index character similar Strings
res = list(filter(lambda x: x[K-1].lower() == check.lower(), test_list))
 
# print result
print("The list of matching Kth letter : " + str(res))


Output

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

Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(k), where k is the number of elements that satisfy the condition.



Last Updated : 28 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads