How can I concatenate a list

python concatenate multiple lists
python concatenate list of lists
list concat c#
python concatenate list of strings
combine elements of two lists python
concatenate two lists python
python combine two lists into list of lists
python merge multiple lists

I need to concatenate these values, I've seen examples using string builder but I cant quite figure it.

I am trying to return recreate the linestrings of https://api.tfl.gov.uk/Line/140/Route/Sequence/Inbound

However the results I have to return, have more than 1 string of co-ords hence the adding "[", "]"

//

for (int i = 0; i < R.geometry.coordinates.Count; i++)

    foreach (List<List<double>> C in R.geometry.coordinates)

    {

        RS.lineStrings.Add(i++.ToString());
        RS.lineStrings.Add("[");
        foreach (List<double> a in C)
        {   
       // These values are to be concatentated, I'm wanting to create a string of RS.lineStrings.Add($"[{a[1]},{a[0]}]");
            RS.lineStrings.Add($"[{a[1]},{a[0]}]");  
        }

        RS.lineStrings.Add("]");
        RS.lineStrings.Add(",");
}

Considering in your code C is List<List<double>>. Then you can use LINQ to concatenate

var sb = new StringBuilder(C.Count * 20); // appox length not to resize

C.ForEach(item => sb.AppendFormat("[{0},{1}]", item[1], item[0]));

var str = sb.ToString(); // This is concatenation.

If you want list of strings

C.Select(item => $"[{item[1]},{item[0]}]").ToList();   

Based on your new update (I am trying to return "[[0,1],[2,3],[4,5]]") do this

var result = "[" + string.Join(",", C.Select(item => $"[{item[1]},{item[0]}]")) + "]";

Which method you choose - should depend on the details of your list. you can still do it with string builder for better memory management

var sb = new StringBuilder(C.Count * 20); // appox length not to resize

C.ForEach(item => sb.AppendFormat("[{0},{1}],", item[1], item[0])); // note comma - ],
sb.Insert(0, "[").Replace(',', ']', sb.Length - 1, 1); // this removes last comma, ads closing bracket

Concatenating two lists in Python - Noteworthy, extend operator can be used to concatenate one list into another. It is not possible to store the value into the third list. One of the existing list has to store the  Method #3 : Using list comprehension. List comprehension can also accomplish this task of list concatenation. In this case, a new list is created, but this method is a one liner alternative to the loop method discussed above.

You can use string.Join() to join them:

string result = string.Join(",", C);

Python Join Two Lists, There are several ways to join, or concatenate, two or more lists in Python. One of the easiest ways are by using the + operator. Example. Join two list: list1 =  Please do as follow: 1. Select a blank cell adjacent to the list's first data, for instance, the cell C1, and type this formula =CONCATENATE 2. Highlight the TRANSPOSE (A1:A7)&"," in the formula, and press the F9 key. 3. Remove curly braces { and } from the formula, and press the Enter key.

Strings are inmutable. So if you do a lot of string connaction, that can leave a lot of dead strings in memory. The GC will deal with them, but it is still a performance issue. Especially on a Webserver it should be avoided. And then there are things like StringInterning too. A lot of minor optimisations that will get in the way if you do mass operations on strings.

StringBuilder is the closest we can get to a mutable string, that get's around those optimsiations (wich may be a hinderance here). The only use difference is that you use "Append" rather then "Add".

How to concatenate Lists In Python?, Let's see all the ways to concatenate lists. Concatenation Operator(+) 1.1 It's a general way to concatenate two lists in Python. Loops. 2.1 The next way to concatenate lists is using the loops. List Comprehensions. * Operator (Unpacking) extend() Built-in Method. itertools. Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Talent Hire technical talent

There are multiple possibilities :

 RS.lineStrings.Add(string.concat("[{a[1]}" + "," + "{a[0]}]");
 RS.lineStrings.Add(string.concat("[{a[1]}",",","{a[0]}]");

Documentation https://docs.microsoft.com/en-us/dotnet/csharp/how-to/concatenate-multiple-strings

How can I get the concatenation of two lists in Python without , Yes: list1 + list2 . This gives a new list that is the concatenation of list1 and list2 . CONCATENATE(text1,text2,) Text1, text2, are 1 to 30 text items to be joined into a single text item. The text items can be text strings, numbers, or column references. Remark. The "&" operator can be used instead of CONCATENATE to join text items. Example

Here's a solution with a StringBuilder. It's wordy as all get out, but it should be much faster and produce much less garbage for a large number of items. It uses no string concatenation.

 var listOfLists = new List<List<double>>
 {
     new List<double> {1.0, 2.0, 3.0},
     new List<double> {3.14, 42.0}
 };
 var buffer = new StringBuilder();
 buffer.Append('[');
 var firstOuter = true;
 foreach (var list in listOfLists)
 {
     var firstInner = true;
     buffer.Append('[');
     if (!firstOuter)
     {
         buffer.Append(',');
     }
     foreach (var item in list)
     {
         if (!firstInner)
         {
             buffer.Append(',');
         }

         firstInner = firstOuter = false;
         buffer.Append(item.ToString());
     }
     buffer.Append(']');
 }
 buffer.Append(']');

 var concatenated = buffer.ToString();

Python: Concatenate all elements in a list into a string and return it , Python Exercises, Practice and Solution: Write a Python program to concatenate all elements in a list into a string and return it. Here are the steps to concatenate an Excel Range with space as the separator (as shown in the pic): Select the cell where you need the result. Go to formula bar and enter =TRANSPOSE (A1:A5)&” “ Based on your regional settings, you can also try =A1:A5 (instead of Based on your regional settings,

Python Add Lists / Join / Concatenate Two or More Lists, It describes various ways to join/concatenate/add lists in Python. For example – simply appending elements of one list to the tail of the other in a for loop, or using​  =CONCATENATE(B3, " & ", C3) Joins three things: the string in cell B3, a string consisting of a space with ampersand and another space, and the value in cell C3. The result is Fourth & Pine. =B3 & " & " & C3. Joins the same items as the previous example, but by using the ampersand (&) calculation operator instead of the CONCATENATE function

10.16. Append versus Concatenate, Note that the word “cat” needs to be placed in a list since the concatenation operator needs two lists to do its work. 1, origlist = [45,  makes a 2d array by concatenating 2 1d arrays (lists) Similarly. np.array([np.ones(3,3), np.zeros(3,3)]] should produce a (2,3,3) array. An newish np.stack function gives you more control over which axis is added. It works by expanding the dimensions of all input arrays by one, and concatenating. You can expand the dimensions yourself, e.g.

CONCATENATE Excel Range (with and without separator), In this tutorial, learn how the concatenate formula works and how you can quickly concatenate excel range (with or without Creating a Drop Down List. If you want to concatenate a list of numbers into a single string, apply the str() function to each element in the list comprehension to convert numbers to strings, then concatenate them with join(). s = '-'. join ([ str (n) for n in l ]) print (s) # 0-1-2

Comments
  • What didn't you understand about the examples that you saw? What confused you? What didn't work when you tried using them in your situation?
  • Do you have list in list? What type is C ?
  • What exactly do you mean by "concatenate"? Just stick them together, or add some separator (space, comma, end-of-line) between them?
  • You seem to confuse concatenating lists and concatenating strings. What exactly do you want to concatenate? Should the result be a string or a list?
  • In general, if you are working with an arbitrary collection of stuff and you want to concatenate them into a string, you shouldn't be using string concatenation. It's much slower and it generates a lot of garbage that needs to be garbage collected. StringBuilder is much better at this. Just juse StringBuilder.Append and convert the result to a string with ToString at the end.
  • Misses the [x]
  • foreach (List<double> a in C) seems OP has nested list.
  • actually i now see that question is not that clear, so i'm waiting for an edit on question by OP
  • Yeah. And, it gets worse after his update. Deleting this
  • I updated this to match the format that his question now shows - it makes things worse. However, if the number of items in his collections is large, string concatenation is a bad idea (it's slow and garbage producing). If it's small, who cares.
  • yes. But since these people are stingy on telling details, we can be generous and show all the ways to do this simple operation