Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove space after comma in list in python

class Solution:
    def buildArray(self, nums):
    
        ans=[]
        for i in range(len(nums)):
            ans.append(nums[nums[i]])
        return ans

    
sol = Solution()  
res = sol.buildArray([0,2,1,5,3,4])
print(res)

output:

[0, 1, 2, 4, 5, 3]

The problem is in the output , iam sturggling to remove space after comma ‘,’ in the list . Can any body help me with this one

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to turn the list into a string first, which will make that kind of manipulation easier.

Try replacing your last line for this:

res_str = str(res).replace(", ", ',')
print(res_str)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading