{}
run-icon
main.py
products = [ {"name": "Smartphone", "category": "eletrônicos", "price": 150, "discount": 0.10}, {"name": "TV", "category": "eletrônicos", "price": 300, "discount": 0.0}, {"name": "Camiseta", "category": "vestuário", "price": 50, "discount": 0.20}, {"name": "Notebook", "category": "eletrônicos", "price": 1200, "discount": 0.0}, {"name": "Fone de Ouvido", "category": "eletrônicos", "price": 80, "discount": 0.10}, {"name": "Cafeteira", "category": "eletrodomésticos", "price": 200, "discount": 0.0}, ] # Javascript # let l = [] # for(product of products) { # const discount = product.discount; # if(discount > 0) { # const price = product.price; # l.push({ # ...product, # "price": price - price * discount # }) # } # } # Python # l = [ # { # **product, # **{"price": price - price * discount} # } # for product in products # if (discount := product.get("discount")) > 0 and (price := product.get("price")) # ]
Output