🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

sum()

AI & DATA SCIENCE // sum

sum() adds up all the numbers in an iterable, returning their total, with an optional starting value.

Syntax

sum(iterable, start=0)

Deep Dive Course

sum() iterates once over its argument, adding each element to a running total that begins at start, which is 0 by default. It works with any numeric iterable — lists, tuples, generators, ranges — and the start parameter lets you provide a non-zero base for the total, though the += operator or itertools.chain is the more efficient choice for concatenating sequences rather than summing them.

1Understanding sum()

sum() iterates once over its argument, adding each element to a running total that begins at start, which is 0 by default. It works with any numeric iterable — lists, tuples, generators, ranges — and the start parameter lets you provide a non-zero base for the total, though the += operator or itertools.chain is the more efficient choice for concatenating sequences rather than summing them.

💡

sum() only adds numbers efficiently — don't use it to flatten a list of lists, since repeatedly concatenating with + is quadratic time; use itertools.chain.from_iterable() instead.

editor.html
prices = [19.99, 5.50, 3.25]
total = sum(prices)
print(total)
localhost:3000

2Practical Example

Here is a real-world application of sum() showing how it is used in production Python code.

editor.html
orders = [{"item": "Book", "qty": 2}, {"item": "Pen", "qty": 5}]
total_items = sum(order["qty"] for order in orders)
print(total_items)
localhost:3000

3Best Practices

Follow these guidelines when working with sum():

1. Pass a generator expression directly to sum(), instead of building an intermediate list first

2. Use sum() with a generator expression and an if condition to count items matching a condition

3. Avoid sum() for concatenating lists/strings — use ''.join() for strings and itertools.chain for lists

⚠️

Tip: sum() only adds numbers efficiently — don't use it to flatten a list of lists, since repeatedly concatenating with + is quadratic time; use itertools.chain.from_iterable() instead.

editor.html
prices = [19.99, 5.50, 3.25]
total = sum(prices)
print(total)
localhost:3000

Examples

Example 01Basic Usage
prices = [19.99, 5.50, 3.25]
total = sum(prices)
print(total)
Example 02Advanced Example
orders = [{"item": "Book", "qty": 2}, {"item": "Pen", "qty": 5}]
total_items = sum(order["qty"] for order in orders)
print(total_items)

Best Practices

  • Pass a generator expression directly to sum(), instead of building an intermediate list first
  • Use sum() with a generator expression and an if condition to count items matching a condition
  • Avoid sum() for concatenating lists/strings — use ''.join() for strings and itertools.chain for lists

Interview Question

Why is using sum() to flatten a list of lists considered a bad idea?

Hint: Think about what plus does to a list on every iteration.

sum() concatenates each sub-list onto the running total with +, and list concatenation creates a brand-new list each time, copying everything accumulated so far plus the next chunk. Over many sublists this becomes quadratic total work. itertools.chain.from_iterable() (or a nested list comprehension) flattens lazily in linear time without repeatedly copying.

Exercises

MediumPractice using sum() in a real scenario.
View Solution
prices = [19.99, 5.50, 3.25]
total = sum(prices)
print(total)

Frequently Asked Questions

Why is using sum() to flatten a list of lists considered a bad idea?

sum() concatenates each sub-list onto the running total with +, and list concatenation creates a brand-new list each time, copying everything accumulated so far plus the next chunk. Over many sublists this becomes quadratic total work. itertools.chain.from_iterable() (or a nested list comprehension) flattens lazily in linear time without repeatedly copying.

Related Functions

min()max()itertools.chain