Created
December 13, 2023 21:31
-
-
Save jorgevilaca82/bd4c7b290c6d6962dba89b3e1571927a to your computer and use it in GitHub Desktop.
3 ways of sum cart total cost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shopping_cart = [ | |
{"item": "Apple", "price": 0.5, "quantity": 10}, | |
{"item": "Milk", "price": 1.5, "quantity": 2}, | |
{"item": "Bread", "price": 2.0, "quantity": 1} | |
] | |
def item_cost(item): | |
return item["price"] * item["quantity"] | |
sum(map(item_cost, shopping_cart)) | |
sum(map(lambda item: item["price"] * item["quantity"], shopping_cart)) | |
sum([item["price"] * item["quantity"] for item in shopping_cart]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment