Saturday, February 7, 2026
HomeLanguagesPython – Append according to Kth character

Python – Append according to Kth character

Given a String list, append to String i or j value depending on Kth index value.

Input : test_list = [“neveropen”, “best”, “for”, “neveropen”], K = 2, N = ‘e’, i, j = “@@”, “..” 

Output : [‘neveropen..’, ‘best@@’, ‘for@@’, ‘neveropen..’] 

Explanation : neveropen and neveropen having similar 2nd occ. value as ‘e’, hence gets appended by “..”. 

Input : test_list = [“giiksforneveropen”, “bst”, “for”, “neveropen”], K = 2, N = ‘e’, i, j = “@@”, “..” 

Output : [‘giiksforneveropen@@’, ‘best@@’, ‘for@@’, ‘neveropen@@’] 

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 = ["neveropen", "best", "for", "neveropen"]
 
# 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))


Output

The original list : ['neveropen', 'best', 'for', 'neveropen']
The resultant List : ['neveropen**', 'best##', 'for##', 'neveropen**']

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 = ["neveropen", "best", "for", "neveropen"]
 
# 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))


Output

The original list : ['neveropen', 'best', 'for', 'neveropen']
The resultant List : ['neveropen**', 'best##', 'for##', 'neveropen**']

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 = ["neveropen", "best", "for", "neveropen"]
 
# 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))


Output

The original list : ['neveropen', 'best', 'for', 'neveropen']
The resultant List : ['neveropen**', 'best##', 'for##', 'neveropen**']

Time Complexity: O(N)
Auxiliary Space: O(N)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32491 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6862 POSTS0 COMMENTS
Nicole Veronica
11987 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12075 POSTS0 COMMENTS
Shaida Kate Naidoo
6996 POSTS0 COMMENTS
Ted Musemwa
7237 POSTS0 COMMENTS
Thapelo Manthata
6947 POSTS0 COMMENTS
Umr Jansen
6933 POSTS0 COMMENTS