Avoid duplicated operations in lambda functions
<p>I'm using a lambda function to extract the number in a string:</p>

<pre><code>text = "some text with a number: 31"
get_number = lambda info,pattern: re.search('{}\s*(\d)'.format(pattern),info.lower()).group(1) if re.search('{}\s*(\d)'.format(pattern),info.lower()) else None
get_number(text,'number:')
</code></pre>

<p>How can I avoid to make this operation twice?:</p>

<pre><code>re.search('{}\s*(\d)'.format(pattern),info.lower()
</code></pre>

<p>This is super ugly, not going to lie, but it does work and avoids returning a match object if it's found, but does return None when it's not:</p>

<pre><code>lambda info,pattern: max(re.findall('{}\s*(\d)'.format(pattern),info.lower()),[None],key=lambda x: x != [])[0]
</code></pre>

python regex lambda