title: Python 2.** Finding the union of all possible intersections of two-list combinations from a list of lists
body: <p>Disclaimer: I'm teaching myself Python, so there might be some trivial solution to each of my questions. Patience is appreciated!</p>
  
  <p>I understand that the title is a little unclear, so I'll try to clarify with an example.</p>
  
  <p>Suppose we have an array of transactions:</p>
  
  <p><code>txArray=[[u'1'],[u'2'],[u'2', u'3']]</code></p>
  
  <p>The goal is to write a function <code>myIntersection(arrayOfLists)</code> that first calculates the intersection of each possible pair of lists in <code>txArray</code>, and then takes the union. </p>
  
  <p>So <code>myIntersection(txArray)</code> should return <code>[u'2']</code>, because:</p>
  
  <pre><code>int1=intersection([u'1'],[u'2'])=[]
  int2=intersection([u'1'],[u'2', u'3'])=[]
  int3=intersection([u'2'],[u'2', u'3'])=[u'2']
  
  union=(int1 U int2 U int3)=[u'2']
  </code></pre>
  
  <p>What I've tried so far is below:</p>
  
  <pre><code>from itertools import combinations
  
  '''
  Pseudocode:
  1) Generate all possible 2-combinations of the lists in txArray
  2) Flatten the lists
  3) If a value appears more than once in a 2-combination, add it to
  list of intersections
  4) Keep unique elements in list of intersections
  
  '''
  
  def myIntersection(arrayOfLists):
      flat_list=[]
      intersections=[]
      combs=list(combinations(txArray,2))
      for i in range(0, len(combs)):
          flat_list.append([item for sublist in combs[i] for item in sublist])
      for list in flat_list:
          for element in list:
              if list.count(element)&gt;1:
                  if element not in intersections:
                      intersections.append(element)
      return intersections
  </code></pre>
  
  <p>While it works in a python command-line interface, I keep getting errors with this approach when I save it as a python file and run it.</p>
  
  <p>My questions are:
  1) Why doesn't it work when I run it as a python file?</p>
  
  <p>2) Is there a cleaner, more 'pythonic' way to do this (possibly with list comprehensions)</p>
  
  <p>3) I did think about using sets instead, but I couldn't figure out to iteratively convert the lists of arrayofLists (in the general case) to sets. Is there a simple syntax for doing that? </p>
  
  <p>Thank you so much!</p>