tags:

views:

192

answers:

6

I have a list of objects appended from a mysql database and contain spaces. I wish to remove the spaces such as below, but the code im using doesnt work?

hello = ['999 ',' 666 ']

k = []

for i in hello:
    str(i).replace(' ','')
    k.append(i)

print k
+3  A: 
hello = ['999 ', '666 ']
result = map(lambda x: x.strip(), hello)
yedpodtrzitko
+5  A: 

Strings in Python are immutable (meaning that their data cannot be modified) so the replace method doesn't modify the string - it returns a new string. You could fix your code as follows:

for i in hello:
    j = i.replace(' ','')
    k.append(j)

However a better way to achieve your aim is to use a list comprehension. For example the following code removes leading and trailing spaces from every string in the list using strip:

hello = [x.strip(' ') for x in hello]
Mark Byers
+1 for strip. -1 for replace(' ','')
S.Lott
OP wants to remove *all* spaces, not just trailers, so why -1 for `replace(' ','')`? None of the 'strip' methods, much less the other [built-in str methods](http://docs.python.org/library/stdtypes.html#string-methods), do the job (ignoring Rube Goldbergs like `''.join(s.split())`).
Mike DeSimone
@Mike DeSimone: Judging from the OPs example data and question description it's quite likely that he defined his database type as a char(4) and that rstrip is a more suitable function than replace here. It could be that the downvote was for one of the numerous errors in the replace code in an earlier version of my post rather than choice of replace. It's fixed now though.
Mark Byers
Ah, so it isn't a text field that used spaces to separate thousands, millions, etc. that is defined as text because it might contain the occasional keyword? OK, I missed that.
Mike DeSimone
The second element in the OP's list has 5 chars. Leading and trailing space
gnibbler
Since the example shows no internal spaces, I'm suspicious about removing internal spaces.
S.Lott
+1  A: 

String methods return the modified string.

k = [x.replace(' ', '') for x in hello]
Ignacio Vazquez-Abrams
A: 

replace() does not operate in-place, you need to assign its result to something. Also, for a more concise syntax, you could supplant your for loop with a one-liner: hello_no_spaces = map(lambda x: x.replace(' ', ''), hello)

allonym
A: 

Presuming that you don't want to remove internal spaces:

def normalize_space(s):
    """Return s stripped of leading/trailing whitespace
    and with internal runs of whitespace replaced by a single SPACE"""
    # This should be a str method :-(
    return ' '.join(s.split())

replacement = [normalize_space(i) for i in hello]
John Machin
+1  A: 

List comprehension [num.strip() for num in hello] is the fastest.

>>> import timeit
>>> hello = ['999 ',' 666 ']

>>> t1 = lambda: map(str.strip, hello)
>>> timeit.timeit(t1)
1.825870468015296

>>> t2 = lambda: list(map(str.strip, hello))
>>> timeit.timeit(t2)
2.2825958750515269

>>> t3 = lambda: [num.strip() for num in hello]
>>> timeit.timeit(t3)
1.4320335103944899

>>> t4 = lambda: [num.replace(' ', '') for num in hello]
>>> timeit.timeit(t4)
1.7670568718943969
Selinap
`list(map(str.strip, hello))` doesn't make a whole lot of sense since `map` returns a list itself.
ChristopheD
@ChristopheD: In Python 3, map does not return a list.
Selinap