-1

I have the below list

list = ['\n                                A\n                            ', '\n                                B\n                            ', '\n                                C\n                            ', '\n                                D\n                            ']

Seems like list is center aligned and this list is the output of bs4 code I run. How can I remove all the newline characters from this list that the final output looks like

list = ['A','B','C','D']
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
Adam Iqshan
  • 135
  • 2
  • 12

1 Answers1

2

You can use str.strip() to remove leading and trailing whitespace from a string. To apply it to each item in the list you can use a list comprehension:

lst = [item.strip() for item in lst]

or the map() function:

lst = list(map(str.strip, lst))

As a side note, don't name your variable list as it would shadow the built-in function.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378