1

I have a python array containing a mixed set of values that looks somewhat like the example below:

['0012 ', ' 318422 ', ' www.example.com']

My question is, how can I easily remove leading and trailing spaces from values in the array, such that it looks like this:

['0012', '318422', 'www.example.com']
tobias_k
  • 81,265
  • 12
  • 120
  • 179
pulsar100
  • 41
  • 7

2 Answers2

6

Like so

[x.strip() for x in mylist]
YXD
  • 31,741
  • 15
  • 75
  • 115
0

You can use map and strip:

a = ['0012 ', ' 318422 ', ' www.example.com']
clean_a = map(lambda x: x.strip(), a)
user2390182
  • 72,016
  • 6
  • 67
  • 89