title: Loop through .csv files with conditions in different columns
body: <p>I've got a large .csv file with this format:</p>
  
  <p><em>"String 1", "String 2", "String 3", "String 4", "String 5", "String 6", etc</em>  </p>
  
  <p>I am interested in extracting information from a column as long as it is linked to the following one.</p>
  
  <p>To give a more clear example, suppose columns number 3 and 4 are formed by teams, and they represent whom they host (col 3 is the local team).</p>
  
  <p><em>"First", "Result", "Philadelphia", "Miami", etc<br>
  "Second", "Result", "Dallas", "Cleveland", etc<br>
  "Third", "Result", "Miami", "Cleveland", etc<br>
  "Fourth", "Result", "Cleveland", "Miami", etc<br>
  "Fifth", "Result", "Dallas", "Philadelphia", etc<br>
  "Sixth", "Result", "Cleveland", "Dallas", etc<br>
  "Seventh", "Result", "Miami", "Philadelphia", etc<br>
  "Eighth", "Result", "Philadelphia", "Miami", etc<br>
  "Ninth", "Result", "Cleveland", "Miami", etc</em>  </p>
  
  <p>And I want to obtain a list consisting of the teams they host, without repetition</p>
  
  <pre><code>Cleveland hosts
  Dallas
  Miami
  
  Dallas hosts
  Cleveland
  Philadelphia
  
  Miami hosts
  Cleveland
  Philadelphia
  
  Philadelphia hosts
  Miami
  </code></pre>
  
  <p>After that, I'd like to write in a file all rows concerning both patterns, this is, if I wanted to see matches between Cleveland and Miami I would like to have a .csv like this,</p>
  
  <p><em>"Third", "Result", "Miami", "Cleveland", etc<br>
  "Fourth", "Result", "Cleveland", "Miami", etc<br>
  "Ninth", "Result", "Cleveland", "Miami", etc</em></p>
  
  <p>With the following code, I manage to read one column and store all unique elements in a dictionary so that I can choose a word from there later. I could do the same with column number 4 and repeating the code by changing the value of the parameter Wanted_Column to 3</p>
  
  <pre><code>import csv
  from collections import Counter, defaultdict, OrderedDict
  
  Var = 1 
  Wanted_Column = 2 # Col I want to analyze 
  
  with open('file.csv', "rb") as inputfile:
      data = csv.reader(inputfile)
      seen = defaultdict(set)
  
      countd = Counter(
          row[Wanted_Column]
          for row in data
          if row[Wanted_Column] and row[Wanted_Column] not in seen[row[Var]] and not seen[row[Var]].add(row[Wanted_Column])
      )
  
  y = OrderedDict(sorted(countd.items(), key = lambda t: t[0]))
  
  for line in y:
      print line
  </code></pre>
  
  <p>The result is,</p>
  
  <pre><code>Cleveland
  Dallas
  Miami 
  Philadelphia 
  </code></pre>
  
  <p>So, my question is, what should I add to have a double condition and show elements the way I have exposed?</p>
  
  <p>After that, to write rows in another file I've got this code,</p>
  
  <pre><code>look_for = set([ELEMENT IN DICTIONARY])
  
  with open('file.csv','rb') as inf, open('output_file.csv','wb') as outf:
      incsv = csv.reader(inf, delimiter=',')
      outcsv = csv.writer(outf, delimiter=',')
  
      outcsv.writerows(row for row in incsv if row[Wanted_column] in look_for) 
  </code></pre>
  
  <p>And with just one element it works well, but of course, as the previous condition is not well defined, I don't know what should I change to get the result that I want.</p>