title: Merging 2 Lists In Multiple Ways - Python
body: <p>I've been experimenting with a number of techniques but I'm sure there's smooth-ish way to get this done.</p>
  
  <p>Suppose I have two lists with the same amount of items in them (4 each):</p>
  
  <pre><code>a = ['a', 'b', 'c', 'd']    
  b = [1, 2, 3, 4]
  </code></pre>
  
  <p>I'd like to merge these lists in all possible ways while retaining order. Example outputs:</p>
  
  <pre><code>a, b, c, d, 1, 2, 3, 4    
  1, 2, 3, 4, a, b, c, d    
  a, b, 1, 2, c, 3, 4, d
  </code></pre>
  
  <p>The point is each of the lists must retain its order so an item can not precede another item in the output considering its position in the list. so for example the output can not be:</p>
  
  <pre><code>a, b, **d**, c, 1...   &gt; d precedes c whereas c is before d in the original list
  1, **4**, a, b, 3....  &gt; 4 precedes 3 whereas 3 is before 4 in the original list
  </code></pre>
  
  <p>I guess the idea is to merge the second list into the first list in all possible ways. A fully worked example is this:</p>
  
  <pre><code>a = [a, b]    
  b = [1, 2]
  </code></pre>
  
  <p>desired output:</p>
  
  <pre><code>ab12                                                                      
  a1b2                                                                             
  a12b                                                                         
  1ab2                                                                             
  1a2b                                                                          
  12ab
  </code></pre>
  
  <p>How do I go about doing this? Does <code>itertools</code> have a capability to do this in some way? Or is there another way to get this done? Please help!</p>