0

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
  • 78,071
  • 11
  • 109
  • 168
pulsar100
  • 31
  • 6

2 Answers2

4

Like so

[x.strip() for x in mylist]
YXD
  • 30,245
  • 14
  • 70
  • 111
0

You can use map and strip:

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