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 = [“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 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 results 
print("The resultant List : " + str(res))

chevron_right


Output

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

Method #2 : Using list comprehension

This solves this problem in similar manner, just difference being, it’s a shorthand and can be used  as one liner approach to solve this problem.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.