-2

I am wondering how to convert multiple string in a list into one string in a list?

It will be like this:

input:["a", "b", "c"]
output:["a,b,c"]

Thanks a lot!

Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
Roy
  • 9
  • 4
  • why do you want to do this? – rv.kvetch Sep 21 '22 at 04:05
  • Does this answer your question? [How to concatenate (join) items in a list to a single string](https://stackoverflow.com/questions/12453580/how-to-concatenate-join-items-in-a-list-to-a-single-string) – TheFungusAmongUs Sep 21 '22 at 13:18
  • @TheFungusAmongUs thank for link, I saw this post, but it will be output a string not a list. – Roy Sep 23 '22 at 03:23

3 Answers3

2

You can use join()

string = ["a", "b", "c"]
[','.join(string)] #if you want to return it inside a list otherwise
','.join(string)
-1
my_string = ["a", "b", "c"]
new_string = []
for s in my_string:
    new_string.append(s)

print (new_string)

output ['a', 'b', 'c']

  • this just copies over one list to another list, and is a rather inefficient way to do that. I don't see the point in doing that. – rv.kvetch Sep 21 '22 at 04:06
-2

Create a new array, and then add it in a loop, append.

nox
  • 25
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 26 '22 at 08:04