0

I can't remove my whitespace in my list.

invoer = "5-9-7-1-7-8-3-2-4-8-7-9"
cijferlijst = []

for cijfer in invoer:
    cijferlijst.append(cijfer.strip('-'))

I tried the following but it doesn't work. I already made a list from my string and seperated everything but the "-" is now a "".

filter(lambda x: x.strip(), cijferlijst)
filter(str.strip, cijferlijst)
filter(None, cijferlijst)
abc = [x.replace(' ', '') for x in cijferlijst]
user2314737
  • 27,088
  • 20
  • 102
  • 114
54m
  • 719
  • 2
  • 7
  • 18

3 Answers3

4

Try that:

>>> ''.join(invoer.split('-'))
'597178324879'
coder
  • 12,832
  • 5
  • 39
  • 53
3

If you want the numbers in string without -, use .replace() as:

>>> string_list = "5-9-7-1-7-8-3-2-4-8-7-9"
>>> string_list.replace('-', '')
'597178324879'

If you want the numbers as list of numbers, use .split():

>>> string_list.split('-')
['5', '9', '7', '1', '7', '8', '3', '2', '4', '8', '7', '9']
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
1

This looks a lot like the following question: Python: Removing spaces from list objects

The answer being to use strip instead of replace. Have you tried

abc = x.strip(' ') for x in x
Community
  • 1
  • 1
A. Dickey
  • 141
  • 1
  • 1
  • 6