'How to remove spaces from list of integers?
My question seems to be very simple, but I still don't find the right answer for me. Maybe, if I am writing the specific explanation somebody can help me out.
So following case:
I have this list a (if I do print(type(a)) it will also return "class list"), which I have manually inserted into the script. Unfortunately if I do this programmatically it will always return list b in the example, which then isn't recognized correctly by the following program.
a = [1,2,3,4,5,6,7,8,9,10]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
My question is: How do I remove the spaces in list b to be like list a?
Solution 1:[1]
If the lazy automatic formatting doesn't suit your needs, then do it yourself:
print( '[' + (','.join(str(i) for i in b)) + ']' )
Solution 2:[2]
You can convert list to a string and then use the replace function to replace all ', ' to ','.
a = [1, 2, 3]
new_list = str(a)
print(new_list.replace(", ", ","))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
| Solution 2 | Thyodas |
