title: Conditionally access list of tuples and sum
body: <p>I have some data that is contained within a list of tuples. I want to sum one part of each tuple if the other part meets a certain set of conditions. Here is some example data:</p>
  
  <pre><code>var = [("car", '1'), ("dog", '1'), ("mercedes", '1'), ("cat", '1'), ("ferrari", '1'), ("bird", '1')]
  </code></pre>
  
  <p>I have the following code that will allow me access all the numeric data in the above structure:</p>
  
  <pre><code>var = [x[1] for x in var]
  print ",".join(map(lambda x: str(x).strip(), var))
  </code></pre>
  
  <p>This will print out data in the following format:</p>
  
  <pre><code>1,1,1,1,1,1
  </code></pre>
  
  <p>If I instead used <code>x[0]</code> in the list comprehension I would get an output of:</p>
  
  <pre><code>car, dog, mercedes, cat, ferrari, bird
  </code></pre>
  
  <p>What I would like to have though is something that says:</p>
  
  <pre><code>if x[0] == "car" or x[0] == "mercedes" or x[0] == "ferrari" then var2 == x[1] + x[1] + x[1]
  print var2
  </code></pre>
  
  <p>I'm assuming that that above won't work, but I'm not really sure of how to code it in a way that will work.</p>
  
  <p>The above is a simple demonstration. The full string I am parsing is:</p>
  
  <pre><code>[("'goal','corner','rightfoot'", '1'), ("'goal','directfreekick','leftfoot'", '1'), 
  ("'goal','openplay','leftfoot'", '1'), ("'goal','openplay','rightfoot'", '2'), 
  ("'miss','corner','header'", '3'), ("'miss','directfreekick','leftfoot'", '1'), 
  ("'miss','directfreekick','rightfoot'", '1'), ("'miss','openplay','header'", '3'), 
  ("'miss','openplay','leftfoot'", '8'), ("'miss','openplay','rightfoot'", '11')]
  </code></pre>
  
  <p>...and the exact syntax I am using to parse is:</p>
  
  <pre><code>matching = {"'goal','openplay','leftfoot'", "'goal','openplay','rightfoot'", "'goal','corner','leftfoot'", "'goal','corner','rightfoot'"}
  regex2 = [value for key, value in regex2 if key in matching]
  regex2 = sum(int(value) for key, value in regex2 if key in matching)
  print regex2
  </code></pre>
  
  <p>...where regex2 is assigned the value of the list of tuples above. The sum line is the one that causes the error. The line above it prints as so:</p>
  
  <pre><code>['1', '1', '2']
  </code></pre>