VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-replace-all-characters-of-a-list-except-the-given-character/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 2 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.


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.


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


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

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. 

Comment