Python | Split strings in a list with the same prefix in all elements



Method # 1: Using List Comprehension + List Slicing

This task can be accomplished using List Comprehension and List Slicing. A list of lists can be used to remove unwanted letters, and a list comprehension can be used to extend logic to the entire line.

# Python3 demo code
# Split lines in the list
# Using a list comprehension + slicing a list

 
# initializing list

test_list = [ `Rs.25` , `Rs.100` , ` Rs. 143` , `Rs.12` , `Rs.4010` ] < / code>

 
# print original list

print ( "The original list:" + str (test_list))

  
# use list comprehension + list slicing
# Split lines in list

res = [sub [ 3 :] for sub in test_list]

 
# print result

print "The list after string slicing:" + str (res))

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 accomplished using the map function. The task of doing the same thing for each row is handled by the lambda and display functions.

# Python3 demo code
# Split lines in the list
# Using map () + slicing + lambda

 
# initializing list

test_list = [ `Rs.25` , `Rs.100` , ` Rs.143` , `Rs.12` , ` Rs.4010` ]

 
# printing the original list

print ( "The original list:" + str (test_list))

 
# using map () + slicing + lambda
# Split lines in the list

res = list ( map ( lambda sub: sub [ 3 :], test_list))

 
# print result

print ( "The list after string slicing:" + str (res))

Output:

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