Python: match lists in two lists of lists
<p>I have two lists of lists. The first is composed of lists formatted as follows:</p>

<pre><code>listInA = [id, a1, a2, a3]
</code></pre>

<p>The second is composed of lists formatted similarly, with the id first:</p>

<pre><code>listInB = [id, b1, b2, b3]
</code></pre>

<p>Neither list is sorted, and they are not of equal lengths.  What is the best way to make a list of lists, with each list of the format:</p>

<pre><code>listInC = [id, a1, a2, a3, b1, b2, b3]
</code></pre>

<p>where the id's are matched between both lists? Thanks!</p>

<p>The fact that the lists are not sorted and are not of equal length increases the difficulty of coming up with an efficient solution to the problem. However, a quick and dirty solution that would work in the end is definitely still feasible.</p>

<p>The ID seems to be first in both lists. Since this is the case, then for each list <code>a</code> in <code>A</code>, we can get the first element of <code>a</code> and check the lists <code>b</code> in <code>B</code>. If the first elements match, then we can create a list including the remaining elements of <code>a</code> and <code>b</code> and append that to <code>C</code>. In short...</p>

<pre><code>def foo(A, B):
   C = []
   for a in A:
      aID = a[0]
      for b in B:
         if aID == b[0]:
            c = [aID, a[1], a[2], a[3], b[1], b[2], b[3]]
            C.append(c)
   return C
</code></pre>

<p>When dealing with large list sizes for <code>A</code> and <code>B</code>, the efficiency of this solution would drop abysmally, but it should work for reasonably-sized lists.</p>

python join list