title: Python match similar strings and group them together from two lists
body: <p>I have two lists in python:</p>
  <pre><code>A = [&quot;Opn&quot;, &quot;Jan-20 A&quot;, &quot;Feb-20 A&quot;, &quot;Mar-20 A&quot;]
  B = [&quot;Jan-20 B&quot;, &quot;Mar-20 B&quot;, &quot;Apr-20 B&quot;, &quot;Cls&quot;]
  </code></pre>
  <p>I want to group together strings that have a similar month-year and get the output as follows. The strings that do not have a match get grouped with empty strings</p>
  <pre><code>[(&quot;Opn&quot;,&quot;&quot;),(&quot;Jan-20 A&quot;,&quot;Jan-20 B&quot;),(&quot;Feb-20 A&quot;,&quot;&quot;),(&quot;Mar-20 A&quot;,&quot;Mar-20 B&quot;),(&quot;&quot;,&quot;Apr-20 B&quot;),(&quot;&quot;,&quot;Cls&quot;)]
  </code></pre>
  <p>My approach is this. It is O(n^2), was wondering if there is a more efficient way to do so.</p>
  <pre><code>output = []
  pair = False
  for i in A:
      for j in B:
          if i.split()[0] == j.split()[0]:
              output.append((i,j))
              pair = True
              B.remove(j)
              break
      if not(pair):
          output.append((i,''))
      pair = False
  output = output + [(&quot;&quot;,j) for j in B]
  </code></pre>
  <p>What would be an efficient way to do so?</p>