title: Group a list of elements into nested list based on pattern
body: <p>I have a list, <code>List_A</code>, which contains elements with a similar pattern. I am trying to nest the similar items based on the pattern. Example below</p>
  <pre><code>List_A &lt;- list(&quot;Q_2020&quot;, &quot;Q_2021&quot;, &quot;C_2019&quot;, &quot;C_2020&quot;, &quot;K_2020&quot;)
  </code></pre>
  <p>Output:</p>
  <pre><code># List of 5
  # $ : chr &quot;Q_2020&quot;
  # $ : chr &quot;Q_2021&quot;
  # $ : chr &quot;C_2019&quot;
  # $ : chr &quot;C_2020&quot;
  # $ : chr &quot;K_2020&quot;
  </code></pre>
  <p>Desired Output:</p>
  <pre><code># List of 3
  # $ Q:List of 2
  #  ..$ : chr &quot;Q_2020&quot;
  #  ..$ : chr &quot;Q_2021&quot;
  # $ C:List of 2
  # ..$ : chr &quot;C_2019&quot;
  # ..$ : chr &quot;C_2020&quot;
  # $ K: chr &quot;K_2020&quot;
  </code></pre>
  <p>I know the pattern would be before <code>&quot;_&quot;</code> so I am using <code>sub()</code> to get the pattern - but I would like some input on how I can create the nested lists. My attempt is a bit too noisy: 1) extract pattern i.e. Q, K, C, 2) create nested list skeleton, 3) Loop an if statement.</p>
  <p>Any input is appreciated.</p>