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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output

The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement : ['G', '$', 'G', '$', '$', '$', '$', '$', '$']

Method #2 : Using map() + lambda

In this, we use map() and lambda function to extend the logic to each element of the list.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output

The original list : ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T']
List after replacement : ['G', '$', 'G', '$', '$', '$', '$', '$', '$']

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.