Python - Append according to Kth character
Last Updated :
28 Apr, 2023
Given a String list, append to String i or j value depending on Kth index value.
Input : test_list = ["geeksforgeeks", "best", "for", "geeks"], K = 2, N = 'e', i, j = "@@", ".."
Output : ['geeksforgeeks..', 'best@@', 'for@@', 'geeks..']
Explanation : geeksforgeeks and geeks having similar 2nd occ. value as 'e', hence gets appended by "..".
Input : test_list = ["giiksforgeeks", "bst", "for", "geeks"], K = 2, N = 'e', i, j = "@@", ".."
Output : ['giiksforgeeks@@', 'best@@', 'for@@', 'geeks@@']
Explanation : No values with K value 'e', all appended by @@.
Method #1: Using loop
This is a brute way to solve this problem, we check for each string's Kth index, if found to be N, then i value is appended else j is appended.
Python3
# Python3 code to demonstrate working of
# Append according to Kth character
# Using loop
# initializing lists
test_list = ["geeksforgeeks", "best", "for", "geeks"]
# printing string
print("The original list : " + str(test_list))
# initializing K
K = 2
# initializing N
N = 'e'
# initializing i, j
i, j = "**", "##"
res = []
for sub in test_list:
# checking for Kth index to be N
if sub[K] == N:
res.append(sub + i)
else :
res.append(sub + j)
# Printing the resultant list
print("The resultant List : " + str(res))
OutputThe original list : ['geeksforgeeks', 'best', 'for', 'geeks']
The resultant List : ['geeksforgeeks**', 'best##', 'for##', 'geeks**']
Time complexity: O(n), where n is the length of the test_list. The loop takes O(n) time.
Auxiliary Space: O(n), extra space of size n is required.
Method #2: Using list comprehension
This solves this problem in a similar manner, just the difference being, it's a shorthand and can be used as a one-liner approach to solve this problem.
Python3
# Python3 code to demonstrate working of
# Append according to Kth character
# Using list comprehension
# Initializing lists
test_list = ["geeksforgeeks", "best", "for", "geeks"]
# Printing string
print("The original list : " + str(test_list))
# Initializing K
K = 2
# Initializing N
N = 'e'
# initializing i, j
i, j = "**", "##"
# shorthand to solve this problem
res = [sub + i if sub[K] == N else sub + j for sub in test_list]
# Printing results
print("The resultant List : " + str(res))
OutputThe original list : ['geeksforgeeks', 'best', 'for', 'geeks']
The resultant List : ['geeksforgeeks**', 'best##', 'for##', 'geeks**']
The Time and Space Complexity for all the methods is the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using map() with lambda function
Python3
# Python3 code to demonstrate working of
# Append according to Kth character
# Using map() with lambda function
# initializing lists
test_list = ["geeksforgeeks", "best", "for", "geeks"]
# printing string
print("The original list : " + str(test_list))
# initializing K
K = 2
# initializing N
N = 'e'
# initializing i, j
i, j = "**", "##"
# Appending string
# using map() with lambda function
res = list(map(lambda sub: sub + i if sub[K] == N else sub + j, test_list))
# Printing results
print("The resultant List : " + str(res))
OutputThe original list : ['geeksforgeeks', 'best', 'for', 'geeks']
The resultant List : ['geeksforgeeks**', 'best##', 'for##', 'geeks**']
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Python | Append K character N times Sometimes, we wish to manipulate a string in such a way in which we might need to add additional K at the end of string in case of filling the missing bits or any other specific requirement. The solution to this kind of problems is always handy and is good if one has knowledge of it. Letâs discuss c
4 min read
Python | Alternate character addition Sometimes, while working with Python, we can have a problem in which we need to add a character after every character in String. This kind of problem can have its application in many day-day programming domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop Th
5 min read
Python | Add leading K character Sometimes, during the string manipulation, we are into a problem where we need to pad or add leading K to the string as per the requirements. This problem can occur in web development. Having shorthands to solve this problem turns to be handy in many situations. Letâs discuss certain ways in which t
4 min read
Python | Merge adjacent Digit characters Sometimes, more than one type of data can come in python list and sometimes its undesirably tokenized and hence we require to join the digits that has been tokenized and leave the alphabets as they are. Letâs discuss certain ways in which this task can be achieved. Method #1 : Using list comprehensi
3 min read
Python - Remove N characters after K Given a String, remove N characters after K character. Input : test_str = 'ge@987eksfor@123geeks is best@212 for cs', N = 3, K = '@' Output : 'geeksforgeeks is best for cs' Explanation : All 3 required occurrences removed. Input : test_str = 'geeksfor@123geeks is best for cs', N = 3, K = '@' Output
2 min read