Python | Kth index character similar Strings
Last Updated :
28 Apr, 2023
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))
OutputThe 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))
OutputThe 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.
Similar Reads
Python - Similar characters Strings comparison Given two Strings, separated by delim, check if both contain same characters. Input : test_str1 = 'e!e!k!s!g', test_str2 = 'g!e!e!k!s', delim = '!' Output : True Explanation : Same characters, just diff. positions. Input : test_str1 = 'e!e!k!s', test_str2 = 'g!e!e!k!s', delim = '!' Output : False Ex
6 min read
Python | Strings with similar front and rear character Sometimes, while programming, we can have a problem in which we need to check for the front and rear characters of each string. We may require to extract the count of all strings with similar front and rear characters. Let's discuss certain ways in which this task can be performed. Method #1: Using
4 min read
Python - Lowercase Kth Character in string The problem of lowercasing a string is quite common and has been discussed many times. But sometimes, we might have a problem like this in which we need to convert the Nth character of string to lowercase. Letâs discuss certain ways in which this can be performed. Method #1 : Using string slicing +
4 min read
Python - Groups Strings on Kth character Sometimes, while working with Python Strings, we can have a problem in which we need to perform Grouping of Python Strings on the basis of its Kth character. This kind of problem can come in day-day programming. Let's discuss certain ways in which this task can be performed. Method #1: Using loop Th
4 min read
Python - Test if Kth character is digit in String Given a String, check if Kth index is a digit. Input : test_str = 'geeks9geeks', K = 5 Output : True Explanation : 5th idx element is 9, a digit, hence True.Input : test_str = 'geeks9geeks', K = 4 Output : False Explanation : 4th idx element is s, not a digit, hence False. Method #1: Using in operat
5 min read