Open In App

Python program to Replace all Characters of a List Except the given character

Last Updated : 02 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Replace all Characters Except K
# Using list comprehension and conditional expressions
 
# initializing lists
test_list = ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing repl_chr
repl_chr = '$'
 
# initializing retain chararter
ret_chr = 'G'
 
# list comprehension to remake list after replacement
res = [ele if ele == ret_chr else repl_chr for ele in test_list]
 
# printing result
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




# Python3 code to demonstrate working of
# Replace all Characters Except K
# Using map() + lambda
 
# initializing lists
test_list = ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing repl_chr
repl_chr = '$'
 
# initializing retain chararter
ret_chr = 'G'
 
# using map() to extend logic to each element of list
res = list(map(lambda ele: ret_chr if ele == ret_chr else repl_chr, test_list))
 
# printing result
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




# Python3 code to demonstrate working of
# Replace all Characters Except K
# Using for loop
 
# initializing lists
test_list = ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing repl_chr
repl_chr = '$'
 
# initializing retain chararter
ret_chr = 'G'
 
# creating an empty list to store the modified characters
res = []
 
# iterating through the list and replacing the characters
for ele in test_list:
    if ele == ret_chr:
        res.append(ele)
    else:
        res.append(repl_chr)
 
# printing result
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 

  1. Define the input list: test_list = [‘G’, ‘F’, ‘G’, ‘I’, ‘S’, ‘B’, ‘E’, ‘S’, ‘T’]
  2. Define the character to replace non-matching characters: repl_chr = ‘$’
  3. Define the character to retain: ret_chr = ‘G’
  4. Use a list comprehension to iterate over each element ele in the test_list.
  5. For each element, check if it is equal to ret_chr.
  6. If it is equal, replace it with ret_chr using the replace() function: ele.replace(ele, ret_chr).
  7. If it is not equal, replace it with repl_chr using the replace() function: ele.replace(ele, repl_chr).
  8. The resulting list of replaced characters is stored in res.
  9. Print the original list: print(“The original list:”, test_list).
  10. 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. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads