Skip to content

Instantly share code, notes, and snippets.

@artturijalli
Created January 19, 2022 18:27
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 artturijalli/8b0153c5950f2ebf9a8cab23b87c952d to your computer and use it in GitHub Desktop.
Save artturijalli/8b0153c5950f2ebf9a8cab23b87c952d to your computer and use it in GitHub Desktop.
# How would you count the numbers in this list in as few lines of code as possible?
numbers = [1, 2, 3, 4, 5]
# 1. Charlie sums the numbers up using a for loop:
total = 0
for number in numbers:
total += number
# 2. Bob sums the numbers up using the reduce function:
from functools import reduce
total = reduce(lambda x, y: x + y, numbers)
# 3. Alice sums up the numbers with the sum function:
total = sum(numbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment