Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation from within a single list

Tags:

People also ask

How do I concatenate items in a list to a single string?

Using join() method to concatenate items in a list to a single string. The join() is an inbuilt string function in Python used to join elements of the sequence separated by a string separator. This function joins elements of a sequence and makes it a string.

How do you concatenate items in a list?

The most conventional method to perform the list concatenation, the use of “+” operator can easily add the whole of one list behind the other list and hence perform the concatenation.

How do you merge all strings in a list in python?

The string join() method returns a string by joining all the elements of an iterable (list, string, tuple), separated by the given separator.

How do you join words in a list in python?

Use str. join() to join a list of strings. Call str. join(iterable) to join each element in the list of strings iterable with each element connected by the separator str .


Scala is new to me so I'm not sure the best way to go about this.

I need to simply take the strings within a single list and join them. So, concat(List("a","b","c")) returns abc.

Should I first see how many strings there are in the list, that way I can just loop through and join them all? I feel like that needs to be done first, that way you can use the lists just like an array and do list[1] append list[2] append list[3], etc..

Edit:

Here's my idea, of course with compile errors..

def concat(l: List[String]): String = {
var len = l.length
var i = 0
    while (i < len) {
        val result = result :: l(i) + " "
    }
result
}