Home > Mobile >  Removing spaces from multiple string items in a list in python
Removing spaces from multiple string items in a list in python

Time:03-31

I have a list of string items in python:

positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']

I want to remove the spaces from the items which contain spaces (e.g. ' RAMF').

I have tried this using the following code:

positions = [x.strip(' ') for x in positions]
# AttributeError: 'NoneType' object has no attribute 'strip'

player_positions = [x.text.strip(' ') for x in player_positions]
# AttributeError: 'str' object has no attribute 'text'

positions = map(str.strip, positions)
# <map at 0x7fde15d19a90>

How can I remove the spaces in any list item that contains a space here?

CodePudding user response:

Solution (Edited):

positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']    
positions = [x.strip(" ") for x in positions]

CodePudding user response:

I speculate that maybe your actual list contains some items which are not strings. Flush them out using this list comprehension:

bad_positions = [p for p in positions if not isinstance(p, str)]

CodePudding user response:

The question may be ambiguous so I'll offer 3 options:

positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']

# replace all/any spaces
positions = [p.replace(' ', '') for p in positions]
# remove leading and trailing spaces
positions = [p.strip(' ') for p in positions]
# remove leading and trailing whitespace
positions = [p.strip() for p in positions]

CodePudding user response:

Your given list

positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']

and codes will not result in all of your errors. Especially

positions = [x.strip(' ') for x in positions]

would simply work.


This can be deducted from your messages:

positions = [x.strip(' ') for x in positions]
# AttributeError: 'NoneType' object has no attribute 'strip'

The error tells you that there is at least one element in positions that is None - and not a string.

player_positions = [x.text.strip(' ') for x in player_positions]
# AttributeError: 'str' object has no attribute 'text'

The error tells you that there is at least one element in player_positions that is a string - strings do not have an attribute text. If player_positions is the same as positions it would mean that there is at least 1 string value before the None value.

positions = map(str.strip, positions)
# <map at 0x7fde15d19a90>

map in python returns an iterator - the function is applied to your values at the time you iterate it - you don't thats why you get no exception yet. If iterated this gives a similar error to the 1st list._


You would get ALL the errors by doing:

p = ['CF', None, 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']

try:
    p1  = [x.strip(' ') for x in p]
except Exception as e:
    print(type(e), e)

==> <class 'AttributeError'> 'NoneType' object has no attribute 'strip'

try:
    p2  = [x.text.strip(' ') for x in p]
except Exception as e:
    print(type(e), e)

==> <class 'AttributeError'> 'str' object has no attribute 'text'

try:
    p3  = map(str.strip, p)
    p3l = list(p3)
except Exception as e:
    print(type(e), e)

==> <class 'TypeError'> descriptor 'strip' for 'str' objects doesn't apply to a 'NoneType' object which is similar to <class 'AttributeError'> 'NoneType' object has no attribute 'strip'


Filtering out the None value from your data is probably your best guess:

filtered_pos = [p for p in positions if p is not None]

In case you only have strings and None in your data, you should be fine.

If you need to keep the non-strings/None intact, apply strip() only to strings:

p = ['CF', None, 'LCMF', 42, 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']

p1  = [x.strip(' ') if isinstance(x, str) else x for x in p]

print(p1)

will change only strings and leave the others alone:

['CF', None, 'LCMF', 42, 'AMF', 'LW', 'RAMF', 'LCMF', 'AMF', 'RB']

CodePudding user response:

Try to use string replace function as I mentioned below.

positions = ['CF', 'LCMF', 'RW', 'AMF', 'LW', ' RAMF', ' LCMF', ' AMF', ' RB']

print([position.replace(' ','') for position in positions])
  • Related