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
  • 24,359
  • 17
  • 91
  • 104
54m
  • 631
  • 2
  • 7
  • 17

3 Answers3

4

Try that:

>>> ''.join(invoer.split('-'))
'597178324879'
coder
  • 12,597
  • 5
  • 34
  • 51
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
  • 43,657
  • 11
  • 92
  • 117
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