title: Python lambda if statement re.sub
body: <p>So I am using the following regex to parse text and grab information from a specific dictionary:</p>
  
  <pre><code>re.sub(r'(&lt;Q\d+&gt;)',lambda m: quotes[m.group(1)][1],text)
  </code></pre>
  
  <p>What I want to do, is only have it replace if what it would replace with is a key in a separate dictionary. Logically it would look like this:</p>
  
  <pre><code>re.sub(r'(&lt;Q\d+&gt;)',lambda m: quotes[m.group(1)][1] if quotes[m.group(1)][1] in d,text)
  </code></pre>
  
  <p>now if I were to run the following, I get the following syntax error:</p>
  
  <pre><code>&gt;&gt;&gt; re.sub(r'(&lt;Q\d+&gt;)',lambda m: quotes[m.group(1)][1] if quotes[m.group(1)][1] in d,text)
    File "&lt;stdin&gt;", line 1
      re.sub(r'(&lt;Q\d+&gt;)',lambda m: quotes[m.group(1)][1] if quotes[m.group(1)][1] in d,text)
                                                                                      ^
  SyntaxError: invalid syntax
  </code></pre>
  
  <p><strong>How can I only replace in this way?</strong></p>