[Python] Match dict values and add a key to the dict if it matchs

Discussão em 'Python' iniciado por Stack, Agosto 20, 2025.

  1. Stack

    Stack Membro Participativo

    I have two lists of dicts.

    products = [
    {
    'offer': {
    'name': 'Iphone',
    'sku': '1234'
    }
    },
    {
    'offer': {
    'name': 'Samsung',
    'sku': '5678'
    }
    },
    ]


    prices = [
    {
    'id': '1234',
    'price': {
    'value': 500,
    'currencyId': 'USD'
    }
    },
    {
    'id': '5678',
    'price': {
    'value': 600,
    'currencyId': 'USD'
    }
    }
    ]


    I have to add prices to products with matching them by fields "sku" and "id". I want to get a new list of dicts

    [
    {
    'offer': {
    'name': 'Iphone',
    'sku': '1234',
    'value': 500,
    'currencyId': 'USD'
    }
    },
    {
    'offer': {
    'name': 'Samsung',
    'sku': '5678',
    'value': 600,
    'currencyId': 'USD'
    }
    }
    ]


    I tried to do this:

    for product, price in zip(products, prices):
    if product.get("offer", {}).get("sku", {}) == price.get("id", {}):
    product.get("offer", {}).update(price.get("price", {}))


    but it doesn't works. It updates only 14 products (in the list of 4000 products) and I can't understand why.

    Continue reading...

Compartilhe esta Página