title: Split string to dictionary as key,list
body: <p>I have a string type which will return thousands of records in the format</p>
  
  <p><code>key1,val1,val2,val3,val4,val5:key2,val6,val7,val8,val9,val10:key3,val11,val12,val13,val14,val15</code></p>
  
  <p>I want to assign this to a dictionary as Key,List so it looks like</p>
  
  <p>key1,[val1,val2,val3,val4,val5]</p>
  
  <p>key2,[val6,val7,val8,val9,val10]</p>
  
  <p>key3,[val11,val12,val13,val14,val15]</p>
  
  <p>.
  .
  .</p>
  
  <p>All keys are unique in the string and the List size is constant for all records.</p>
  
  <p>At the moment I'm using Split and looping each record using</p>
  
  <pre><code>    //short example string - may contain 1000's
      string newstr = @"key1,val1,val2,val3,val4,val5:key2,val6,val7,val8,val9,val10:key3,val11,val12,val13,val14,val15";
  
      Dictionary&lt;string, List&lt;string&gt;&gt; mydictionary = new Dictionary&lt;string, List&lt;string&gt;&gt;();
      foreach (string item in newstr.Split(':'))
      {
          List&lt;string&gt; list = new List&lt;string&gt;(item.Split(','));
          mydictionary.Add(list[0], list);        
      }
  </code></pre>
  
  <p>My question is, is there a more efficient/quicker way of doing this for 1000's of records using C#4.0 rather than looping?</p>
  
  <p><strong>UPDATE:</strong> having tested the various answers the following are the 'correct' times</p>
  
  <p><img src="https://i.stack.imgur.com/hIbhS.png" alt="enter image description here"></p>
  
  <pre><code>static void Main(string[] args)
  {
      System.IO.StreamReader myFile =  new System.IO.StreamReader(@"C:\Users\ooo\Desktop\temp.txt");
      string newstr = myFile.ReadToEnd();
      myFile.Close();
  
      TimeSpan ts;
      TimeSpan te;
      Stopwatch stopWatch = new Stopwatch();
      stopWatch.Start();
  
  
      ts = stopWatch.Elapsed;
      Dictionary&lt;string, List&lt;string&gt;&gt; mydictionary = new Dictionary&lt;string, List&lt;string&gt;&gt;();
      foreach (string item in newstr.Split(':'))
      {
          List&lt;string&gt; list = new List&lt;string&gt;(item.Split(','));
          mydictionary.Add(list[0], list);
      }
      te = stopWatch.Elapsed;
      Console.WriteLine("MyTime: " + (te - ts).ToString());
  
  
  
      ts = stopWatch.Elapsed;
      var result = newstr.Split(':')
           .Select(line =&gt; line.Split(','))
           .ToDictionary(bits =&gt; bits[0],
                         bits =&gt; bits.Skip(1).ToList());
      te = stopWatch.Elapsed;
      Console.WriteLine("JonSkeet: " + (te - ts).ToString());
  
  
      ts = stopWatch.Elapsed;
      string[] keysAndValues = newstr.Split(':');
      var newdictionary = new Dictionary&lt;string, List&lt;string&gt;&gt;(keysAndValues.Length);
      foreach (string item in keysAndValues)
      {
          List&lt;string&gt; list = new List&lt;string&gt;(item.Split(','));
          newdictionary.Add(list[0], list);
      }
      te = stopWatch.Elapsed;
      Console.WriteLine("Joe: " + (te - ts).ToString());
  
  
      Console.WriteLine("Records: " + mydictionary.Count.ToString());
  
  
      stopWatch.Stop();
  }
  </code></pre>