PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 12, 2022

[FIXED] How to remove space after comma in list in python

 May 12, 2022     append, arrays, python     No comments   

Issue

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


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)


Answered By - Johannes
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing