1

I have a list of strings, and I want to remove all texts after the "\n" in that list each row (including "\n"). How can I do that?

I tried to use the partition function, but it doesn't work with the list...

my_list =["Today is a good day.\nLet's go outside","I have a cat to feed.\nI need go home now."]

Expected result:

my_list =['Today is a good day.','I have a cat to feed.']

Thanks for all the help

BENY
  • 317,841
  • 20
  • 164
  • 234

1 Answers1

1

With the list you gave:

my_list = ['Today is a good day.\nLet\'s go outside', 'I have a cat to feed.\nI need go home now.']
my_list = [item.split('\n')[0] for item in list]
# -> ['Today is a good day.', 'I have a cat to feed.']
whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44