Python | Split strings in list with same prefix in all elements
Sometimes we face a problem in which we have a list of strings and there are some garbage/unwanted letters at its prefix or suffix or at the specified position uniformly, i.e this extends to all the strings in the list. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension + list slicing
This task can be performed using list comprehension and list slicing. List slicing can be used to remove the unwanted letters and list comprehension can be used to extend the logic to the entire string.
# Python3 code to demonstrate # Split strings in list # Using list comprehension + list slicing # initializing list test_list = ['Rs.25', 'Rs.100', 'Rs.143', 'Rs.12', 'Rs.4010'] # printing original list print("The original list : " + str(test_list)) # using list comprehension + list slicing # Split strings in list res = [sub[3:] for sub in test_list] # print result print("The list after string slicing : " + str(res)) |
The original list : ['Rs.25', 'Rs.100', 'Rs.143', 'Rs.12', 'Rs.4010'] The list after string slicing : ['25', '100', '143', '12', '4010']
Method #2 : Using map() + slicing + lambda
This particular task can be performed using map function as well. The task of performing the same for each string is handled by lambda function and map function.
# Python3 code to demonstrate # Split strings in list # Using map() + slicing + lambda # initializing list test_list = ['Rs.25', 'Rs.100', 'Rs.143', 'Rs.12', 'Rs.4010'] # printing original list print("The original list : " + str(test_list)) # using map() + slicing + lambda # Split strings in list res = list(map(lambda sub: sub[3:], test_list)) # print result print("The list after string slicing : " + str(res)) |
The original list : ['Rs.25', 'Rs.100', 'Rs.143', 'Rs.12', 'Rs.4010'] The list after string slicing : ['25', '100', '143', '12', '4010']
Recommended Posts:
- Python | Pandas Split strings into two List/Columns using str.split()
- Python | Remove prefix strings from list
- Python | Append suffix/prefix to strings in list
- Python | Split strings and digits from string list
- Python | Split list of strings into sublists based on length
- Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
- Python | Convert case of elements in a list of strings
- Python | Split Sublist Strings
- Python | Reversed Split Strings
- Python | Merging two strings with Suffix and Prefix
- Python | Ways to determine common prefix in set of strings
- Python | Ways to split strings on Uppercase characters
- Python | Ways to split strings using newline delimiter
- Python | Split CamelCase string to individual strings
- Python | Prefix sum list
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.

