Skip to content

Instantly share code, notes, and snippets.

@jorgevilaca82
Created December 13, 2023 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgevilaca82/bd4c7b290c6d6962dba89b3e1571927a to your computer and use it in GitHub Desktop.
Save jorgevilaca82/bd4c7b290c6d6962dba89b3e1571927a to your computer and use it in GitHub Desktop.
3 ways of sum cart total cost
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