Python program to Replace all Characters of a List Except the given character
Last Updated :
02 Jun, 2023
Given a List. The task is to replace all the characters of the list with N except the given character.
Input : test_list = [‘G’, ‘F’, ‘G’, ‘I’, ‘S’, ‘B’, ‘E’, ‘S’, ‘T’], repl_chr = ‘*’, ret_chr = ‘G’
Output : [‘G’, ‘*’, ‘G’, ‘*’, ‘*’, ‘*’, ‘*’, ‘*’, ‘*’]
Explanation : All characters except G replaced by *
Input : test_list = [‘G’, ‘F’, ‘G’, ‘B’, ‘E’, ‘S’, ‘T’], repl_chr = ‘*’, ret_chr = ‘G’
Output : [‘G’, ‘*’, ‘G’, ‘*’, ‘*’, ‘*’, ‘*’]
Explanation : All characters except G replaced by *
Method #1 : Using list comprehension + conditional expressions
In this, we perform the task of iteration using list comprehension, and replacements are taken care of using conditional operators.
Python3
test_list = [ 'G' , 'F' , 'G' , 'I' , 'S' , 'B' , 'E' , 'S' , 'T' ]
print ( "The original list : " + str (test_list))
repl_chr = '$'
ret_chr = 'G'
res = [ele if ele = = ret_chr else repl_chr for ele in test_list]
print ( "List after replacement : " + str (res))
|
Output
The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement : ['G', '$', 'G', '$', '$', '$', '$', '$', '$']
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1) additional space is not needed.
In this, we use map() and lambda function to extend the logic to each element of the list.
Python3
test_list = [ 'G' , 'F' , 'G' , 'I' , 'S' , 'B' , 'E' , 'S' , 'T' ]
print ( "The original list : " + str (test_list))
repl_chr = '$'
ret_chr = 'G'
res = list ( map ( lambda ele: ret_chr if ele = = ret_chr else repl_chr, test_list))
print ( "List after replacement : " + str (res))
|
Output
The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement : ['G', '$', 'G', '$', '$', '$', '$', '$', '$']
Method 3: Using a for loop to iterate through the list and replace the characters accordingly
Python3
test_list = [ 'G' , 'F' , 'G' , 'I' , 'S' , 'B' , 'E' , 'S' , 'T' ]
print ( "The original list : " + str (test_list))
repl_chr = '$'
ret_chr = 'G'
res = []
for ele in test_list:
if ele = = ret_chr:
res.append(ele)
else :
res.append(repl_chr)
print ( "List after replacement : " + str (res))
|
Output
The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement : ['G', '$', 'G', '$', '$', '$', '$', '$', '$']
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list, since we are creating a new list to store the modified characters.
Method #4 using the replace() function
Step-by-step approach
- Define the input list: test_list = [‘G’, ‘F’, ‘G’, ‘I’, ‘S’, ‘B’, ‘E’, ‘S’, ‘T’]
- Define the character to replace non-matching characters: repl_chr = ‘$’
- Define the character to retain: ret_chr = ‘G’
- Use a list comprehension to iterate over each element ele in the test_list.
- For each element, check if it is equal to ret_chr.
- If it is equal, replace it with ret_chr using the replace() function: ele.replace(ele, ret_chr).
- If it is not equal, replace it with repl_chr using the replace() function: ele.replace(ele, repl_chr).
- The resulting list of replaced characters is stored in res.
- Print the original list: print(“The original list:”, test_list).
- Print the list after replacement: print(“List after replacement:”, res).
Python3
test_list = [ 'G' , 'F' , 'G' , 'I' , 'S' , 'B' , 'E' , 'S' , 'T' ]
repl_chr = '$'
ret_chr = 'G'
res = [ele.replace(ele, ret_chr) if ele = = ret_chr else ele.replace
(ele, repl_chr) for ele in test_list]
print ( "The original list:" , test_list)
print ( "List after replacement:" , res)
|
Output
The original list: ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement: ['G', '$', 'G', '$', '$', '$', '$', '$', '$']
Time complexity: The time complexity of this method is O(n), where n is the length of the input list test_list.
Auxiliary space: The auxiliary space complexity is O(n), where n is the length of the input list.
Please Login to comment...