John Smith
John Smith

Reputation: 1099

get rid of commas in a list python

I have a list [1,2,4,7,5,2] where I want to get rid of the commas to make it [1 2 4 7 5 2] how would I go about this?

np.random.randint(0,4,12) will print out like [0 3 4 1 3 4 2 1 2 4 3 4] and thats the kind of thing I want :)

Upvotes: 0

Views: 2576

Answers (3)

wrgrs
wrgrs

Reputation: 2569

You could do this:

out = '[' + ' '.join(str(item) for item in numbers)+ ']'
print out 

Upvotes: 3

unutbu
unutbu

Reputation: 879421

In [7]: data = [1,2,4,7,5,2]
In [11]: '[{}]'.format(' '.join(map(str, data)))
Out[11]: '[1 2 4 7 5 2]'

or,

In [14]: str(data).replace(',','')
Out[14]: '[1 2 4 7 5 2]'

Upvotes: 2

ecatmur
ecatmur

Reputation: 157344

If you want a numpy ndarray, use:

np.array([1, 2, 4, 7, 5, 2])

Upvotes: 0

Related Questions