Search code examples
pythonxrange

How to remove spaces from a xrange list - Python


Lets say I have:

hello = list(xrange(100))
print hello

The result is:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10...

I need to remove all those spaces from the list so it can be like:

1,2,3,4,5,6,7,8,9,10...

Solution

  • Since join takes string objects, you need to convert those items explicitly to strings. For example:

    hello = ','.join(list(xrange(100)))
    TypeError: sequence item 0: expected string, int found
    

    So:

    hello = xrange(100)
    print ''.join([str(n) for n in hello])
    

    Note there is no need for the list().