Lambda that searches list and increments
<p>Using a Python <code>lambda</code> can you check whether an element exists in another list (of maps) and also increment a variable? I'm attempting to optimise/refactor my code using a lambda but I've gone and confused myself.</p>

<p>Below is my existing code that I want to convert to a lambda. Is it possible to do this using one lambda or will I need to use 2 lambdas? Any advice how can I convert it to a lambda/s?</p>

<pre><code>current_orders = auth.get_orders() 
# returns [{'id': 'foo', 'price': 1.99, ...}, ...]

deleted_orders = auth.cancel_orders() 
# returns id's of all cancelled orders [{'id': 'foo'}, {'id': 'bar'}, ...]


# Attempting to convert to lambda
n_deleted = 0
for del_order in deleted_orders:
    for order in current_orders:
        if del_order['id'] == order['id']:
            n_deleted += 1

# lambda
n_deleted = filter(lambda order, n: n += order['id'] in current_orders, deleted_orders)
# end


if n_deleted != len(orders):
    logger.error("Failed to cancel all limit orders")
</code></pre>

<p><em>Note:</em> I know I can say <code>if len(deleted_orders) &lt; len(current_orders): logger.error("Failed to delete ALL orders")</code> but I want to expand my lambda eventually to say <code>...: logger.error("Failed to delete ORDER with ID: %s")</code></p>

<p>Probably you should be using a list comprehension. eg</p>

<pre><code>current_order_ids = {order['id'] for order in current_orders}
not_del = [order for order in deleted_orders if order['id'] not in current_order_ids]

for order in not_del:
    logger.error("Failed to delete ORDER with ID: %s", order['id'])
</code></pre>

python lambda