Sometimes while working with data, we can have a problem in which we need to perform replace substrings with the mapped string to form a shortform of some terms. This kind of problem can have application in many domains involving data. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop +
replace() + enumerate()
The combination of above functions can be used to perform this task. In this, we perform the task of iteration using loop and enumerate() and replacement with a shorter form is done using replace().
The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science'] The original list 2 is : [['Geeks', 'Gks'], ['And', '&'], ['Computer', 'Comp']] The list after replacement : ['GksforGks', 'is', 'Best', 'For', 'Gks', '&', 'Comp Science']
Method #2 : Using
replace() + list comprehension
This is another way in which this task can be performed. In this, we perform the task of replacing using the replace() and rest of task is performed using list comprehension. It removes list that don’t have replacements.
The original list 1 is : ['GeeksforGeeks', 'is', 'Best', 'For', 'Geeks', 'And', 'Computer Science'] The original list 2 is : [['Geeks', 'Gks'], ['And', '&'], ['Computer', 'Comp']] The list after replacement : ['GksforGks', 'Gks', '&', 'Comp Science']
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.
- Python | Pandas Series.str.replace() to replace text in a series
- replace() in Python to replace a substring
- Python | Replace elements in second list with index of same element in first list
- Python | Convert List of String List to String List
- Python | Replace list elements with its ordinal number
- Python | Replace substring in list of strings
- Python | Replace sublist with other in list
- Python - Replace index elements with elements in Other List
- Python program to Replace all Characters of a List Except the given character
- Python | Convert list of string to list of list
- Python | Grouping similar substrings in list
- Python | Remove Reduntant Substrings from Strings List
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- Python String | replace()
- Python | Replace rear word in String
- Python - Kth word replace in String
- Python | Multiple indices Replace in String
- Python - Replace duplicate Occurrence in String
- Python - Replace all occurrences of a substring in a string
- Python - Replace String by Kth Dictionary value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.