Home > Blockchain >  Python check if key-value match in list of dictionaries
Python check if key-value match in list of dictionaries

Time:11-24

I'd like to check if the prod_id key-value pair in the add_product dictionary matches any of the prod_id key-value pairs in the cart_products list of dictionaries. I'm using a lot of print statements for testing, and I can't get the value for if item in [{product['prod_id']}] == [{cart_products['prod_id']}] to print. Fully runnable code. Any help is appreciated.

product_id = 4
product_name = 'Test name'
product_quantity = 2
product_price = 20

add_product = {'prod_id': product_id, 'prod_name': product_name, 'prod_price': product_price, 'prod_quantity': product_quantity}

cart_products = [{'prod_id': 4, 'prod_name': 'Sloths', 'prod_price': 10, 'prod_quantity': 1}, {'prod_id': 5, 'prod_name': 'Test new id', 'prod_price': 20, 'prod_quantity': 1}, {'prod_id': 6, 'prod_name': 'Add all IDs', 'prod_price': 10, 'prod_quantity': 1}, {'prod_id': 5, 'prod_name': 'Test new id', 'prod_price': 20, 'prod_quantity': 1}, {'prod_id': 6, 'prod_name': 'Add all IDs', 'prod_quantity': 1, 'prod_price': 10}]

in_cart = [{product['prod_id'] for product in cart_products}]
print(in_cart)

for product in cart_products:
    print(f"Show product in cart: {product}")
    for index, value in enumerate(product):
        print(index, value)
    for item in product:
        if item in [{product['prod_id']}] == [{cart_products['prod_id']}]:
            print(f"test test {item}")
        else:
            continue

cart_products  = [add_product]
print(cart_products)
                    

CodePudding user response:

I beleive your requirement is to check, if a product exists in your cart, before adding it to cart. You can follow the below approach for that.

product_id = 4
product_name = 'Test name'
product_quantity = 2
product_price = 20
add_product = {'prod_id': product_id, 'prod_name': product_name, 'prod_price': product_price,
               'prod_quantity': product_quantity}
cart_products = [{'prod_id': 4, 'prod_name': 'Sloths', 'prod_price': 10, 'prod_quantity': 1},
                 {'prod_id': 5, 'prod_name': 'Test new id', 'prod_price': 20, 'prod_quantity': 1},
                 {'prod_id': 6, 'prod_name': 'Add all IDs', 'prod_price': 10, 'prod_quantity': 1},
                 {'prod_id': 5, 'prod_name': 'Test new id', 'prod_price': 20, 'prod_quantity': 1},
                 {'prod_id': 6, 'prod_name': 'Add all IDs', 'prod_quantity': 1, 'prod_price': 10}]


def product_exists(product: dict, cart: list):
    """
    A small method which checks if the product is already in cart!
    :param product: New product to check
    :param cart: Existing cart products
    :return: 
    """
    new_prod_id = product.get("prod_id")
    existing_prod = [ex_prod for ex_prod in cart if ex_prod.get("prod_id") == new_prod_id]
    
    if existing_prod and len(existing_prod) > 0:
        return True, existing_prod

    return False, None


exists, products = product_exists(add_product, cart_products)
if exists:
    print(f"Such a product exists as {products}")
else:
    print("Adding product to cart")
    cart_products.append(add_product)
  • Related