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.



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

The original list : ['Rs.25', 'Rs.100', 'Rs.143', 'Rs.12', 'Rs.4010']
The list after string slicing : ['25', '100', '143', '12', '4010']


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.