How to remove first RemoveThisX\n from array
['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
Trying to remove RemoveThis1\n, RemoveThis2\n, RemoveThis3, RemoveThis4\n
Final result need to be
['DontRemove\nDontRemove','DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove']
CodePudding user response:
a_list = ['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
result = [item[item.find('\n') 1:] for item in a_list]
print(result)
['DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove']
CodePudding user response:
test_list = ['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
result = ["\n".join(item.split("\n")[1:]) for item in test_list]
print(result)
Output will be:
['DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove']
CodePudding user response:
assuming:
initial_list = ['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
I would recommend using either the map function:
mapped_list = list(map(lambda x: x[x.find('\n') 1:], initial_list))
or list comprehension:
comprehended_list = [string[string.find('\n') 1:] for string in initial_list]
Both should produce the asked list.