title: python: complicated loop through list
body: <pre><code>import csv
  import collections
  
  def do_work():
    (data,counter)=get_file('thefile.csv')
    b=samples_subset1(data,counter,'/pythonwork/samples_subset4.csv',500)
    medications_subset2(b,['HYDROCODONE','MORPHINE','OXYCODONE'])
  
  def get_file(start_file):
    with open(start_file,'rb') as f:
      data=list(csv.reader(f))
      counter=collections.defaultdict(int)
  
      for row in data:
        counter[row[10]]+=1
    return (data,counter)
  
  def samples_subset1(data,counter,output_file,sample_cutoff):
    with open(output_file,'wb') as outfile:
      writer=csv.writer(outfile)
      b_counter=0
      b=[]
      for row in data:
        if counter[row[10]]&gt;=sample_cutoff:
          b.append(row) 
          writer.writerow(row)
          b_counter+=1
    return b
  
  def medications_subset2(b,drug_input):
  
    brand_names={'MORPHINE':['ASTRAMORPH','AVINZA','CONTIN','DURAMORPH','INFUMORPH',
                       'KADIAN','MS CONTIN','MSER','MSIR','ORAMORPH',
                       'ORAMORPH SR','ROXANOL','ROXANOL 100'],
           'OXYCODONE':['COMBUNOX','DIHYDRONE','DINARCON','ENDOCET','ENDODAN',
                        'EUBINE','EUCODAL','EUKODAL','EUTAGEN','OXYCODONE WITH ACETAMINOPHEN CAPSULES',
                        'OXYCODONE WITH ASPIRIN,','OXYCONTIN','OXYDOSE','OXYFAST','OXYIR',
                        'PANCODINE','PERCOCET','PERCODAN','PROLADONE','ROXICET',
                        'ROXICODONE','ROXIPRIM','ROXIPRIN','TECODIN','TEKODIN',
                        'THECODIN','THEKOKIN','TYLOX'],
           'OXYMORPHONE':['NUMORPHAN','OPANA','OPANA ER'],
           'METHADONE':['ALGIDON','ALGOLYSIN','AMIDON','DEPRIDOL','DOLOPHINE','FENADONE',
                        'METHADOSE','MIADONE','PHENADONE'],
           'BUPRENORPHINE':['BUPRENEX','LEPTAN','SUBOXONE','SUBUTEX','TEMGESIC'],
           'HYDROMORPHONE':['DILAUDID','HYDAL','HYDROMORFAN','HYDROMORPHAN','HYDROSTAT',
                            'HYMORPHAN','LAUDICON','NOVOLAUDON','OPIDOL','PALLADONE',
                            'PALLADONE IR','PALLADONE SR'],
           'CODEINE':['ACETAMINOPHEN WITH CODEINE','ASPIRIN WITH CODEINE','EMPIRIN WITH CODEINE',
                      'FLORINAL WITH CODEINE','TYLENOL 3','TYLENOL 4','TYLENOL 5']
           'HYDROCODONE':['ANEXSIA','BEKADID','CO-GESIC','CODAL-DH','CODICLEAR-DH',
                          'CODIMAL-DH','CODINOVO','CONATUSSIN-DC','CYNDAL-HD','CYTUSS-HC',
                          'DETUSSIN','DICODID','DUODIN','DURATUSS-HD','ENDAL-HC','ENTUSS',
                          'ENTUSS-D','G-TUSS','HISTINEX-D','HISTINEX-HC','HISTUSSIN-D','HISTUSSIN-HC',
                          'HYCET','HYCODAN','HYCOMINE','HYDROCODONE/APAP','HYDROKON',
                          'HYDROMET','HYDROVO','KOLIKODOL','LORCET','LORTAB',
                          'MERCODINONE','NOROCO','NORGAN','NOVAHISTEX','ORTHOXYCOL',
                          'POLYGESIC','STAGESIC','SYMTAN','SYNKONIN','TUSSIONEX','VICODIN',
                          'VICOPROFEN','XODOL','ZYDONE']}
  
    ...
    ...
  </code></pre>
  
  <p>let's say <code>drug_input = 'METHADONE'</code></p>
  
  <p>i need to be able to go through the b <code>array</code> and delete every row that DOES NOT have ANY ONE of these: </p>
  
  <pre><code>['ALGIDON','ALGOLYSIN','AMIDON','DEPRIDOL','DOLOPHINE','FENADONE',
                        'METHADOSE','MIADONE','PHENADONE'] 
  </code></pre>
  
  <p>for example if <code>b[1] = "yes,no,yes,amidon,blah"</code> then do nothing</p>
  
  <p>but</p>
  
  <p>if <code>b[2] = "yes,yes,yes,vicodin,yes"</code> then DELETE this record</p>