Computing the item price and vat for a given pricing, order, and exchange rate How can I improve the readability of the below function using a list comprehension?
Also, is there a way to improve items() performance?
pricing = {'prices': [{'product_id': 1, 'price': 599, 'vat_band': 'standard'},
                      {'product_id': 2, 'price': 250, 'vat_band': 'zero'},
                      {'product_id': 3, 'price': 250, 'vat_band': 'zero'}],
           'vat_bands': {'standard': 0.2, 'zero': 0}}
order = {'order': {'id': 12, 'items': [{'product_id': 1, 'quantity': 1}, {'product_id': 2,'quantity': 5}]}}
exchange_rate = 1.1

def items():
    &quot;&quot;&quot;
    computes the item price and vat for a given pricing, order, and exchange rate.
    returns list of items dictionaries
    &quot;&quot;&quot;
    return [{'product_id': item['product_id'],
             'quantity': item['quantity'],
             'price': round(product['price'] * exchange_rate, 2),
             'vat': round(pricing['vat_bands']['standard'] * product['price'] * exchange_rate, 2)}
            if product['vat_band'] == 'standard' else
            {'product_id': item['product_id'],
             'quantity': item['quantity'],
             'price': round(product['price'] * exchange_rate, 2),
             'vat': 0}
            for item in order['order']['items'] for product in pricing['prices']
            if item['product_id'] == product['product_id']]


print(items())

Output:
[{'product_id': 1, 'quantity': 1, 'price': 658.9, 'vat': 131.78},
 {'product_id': 2, 'quantity': 5, 'price': 275.0, 'vat': 0}]
As a general rule it is best not to nest comprehensions. It makes code hard to read. You are better off just writing a for loop and appending the results to a list or using a generator.
Here are a couple rules with comprehensions that will make your code less brittle and easy for others to work with:

Don't nest comprehensions.
If a comprehension is too long for one line of code, don't use it.
If you need an else, don't use a comprehension.

Of course there are exceptions to these rules, but they are a good place to start.
One of the reasons nested list comprehension is an issue is it often results in a exponential increase in computation needed. For each item in the order you have to loop through every product. This is not efficient. You want to go from O(n x m) to O(n + m). You should loop through products once and through order items once.
You can see in the updated code below that I loop through the list of products and create a dictionary with the key as the product ID. This makes it so that, while looping through the order items, I can simply get the product by looking up the key. It is much more performant and readable.
pricing = {
    &quot;prices&quot;: [
        {&quot;product_id&quot;: 1, &quot;price&quot;: 599, &quot;vat_band&quot;: &quot;standard&quot;},
        {&quot;product_id&quot;: 2, &quot;price&quot;: 250, &quot;vat_band&quot;: &quot;zero&quot;},
        {&quot;product_id&quot;: 3, &quot;price&quot;: 250, &quot;vat_band&quot;: &quot;zero&quot;},
    ],
    &quot;vat_bands&quot;: {&quot;standard&quot;: 0.2, &quot;zero&quot;: 0},
}
order = {
    &quot;order&quot;: {
        &quot;id&quot;: 12,
        &quot;items&quot;: [{&quot;product_id&quot;: 1, &quot;quantity&quot;: 1}, {&quot;product_id&quot;: 2, &quot;quantity&quot;: 5}],
    }
}
exchange_rate = 1.1


def calculate_exchange_rate(price, rate=None):
    if rate is None:
        rate = exchange_rate
    return round(price * rate, 2)


def items():
    &quot;&quot;&quot;
    computes the item price and vat for a given pricing, order, and exchange rate.
    returns list of items dictionaries
    &quot;&quot;&quot;
    item_list = []
    products = {p[&quot;product_id&quot;]: p for p in pricing[&quot;prices&quot;]}

    for item in order[&quot;order&quot;][&quot;items&quot;]:
        product = products.get(item[&quot;product_id&quot;])
        vat = 0
        if product[&quot;vat_band&quot;] == &quot;standard&quot;:
            vat = pricing[&quot;vat_bands&quot;][&quot;standard&quot;] * product[&quot;price&quot;]
        item_list.append(
            {
                &quot;product_id&quot;: item[&quot;product_id&quot;],
                &quot;quantity&quot;: item[&quot;quantity&quot;],
                &quot;price&quot;: calculate_exchange_rate(product[&quot;price&quot;]),
                &quot;vat&quot;: calculate_exchange_rate(vat),
            }
        )
    return item_list

print(items())