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']
- Python | Replace elements in second list with index of same element in first list
- Python | Grouping similar substrings in list
- Python | Replace sublist with other in list
- Python | Get all substrings of given string
- Python | Replace substring in list of strings
- Python | Get matching substrings in string
- Python String | replace()
- Python | Replace list elements with its ordinal number
- Python - Kth word replace in String
- Python program to print the substrings that are prefix of the given string
- Python - Replace duplicate Occurrence in String
- Python | Multiple indices Replace in String
- Python | Replace rear word in String
- Python - Replace all occurrences of a substring in a string
- Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
- Python | Convert List of String List to String List
- Python | Convert list of string to list of list
- Python | Convert mixed data types tuple list to string list
- Python | Convert string List to Nested Character List
- Python | Convert list of string into sorted list of integer
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.