sum() function in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to add numbers grows as we add more numbers together using the sum() function.
How does the work change when the list of numbers gets bigger?
Analyze the time complexity of the following code snippet.
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total)
This code adds all the numbers in a list and prints the total.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each number in the list one by one.
- How many times: Once for each number in the list.
When the list has more numbers, sum() has to add more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items. Double the items, double the additions.
Time Complexity: O(n)
This means the time to add numbers grows in a straight line with how many numbers there are.
[X] Wrong: "sum() adds all numbers instantly, no matter how many there are."
[OK] Correct: sum() must look at each number to add it, so more numbers mean more work.
Knowing how sum() works helps you understand how simple tasks can grow with input size, a key skill for writing efficient code.
"What if we used sum() on a list of lists instead of numbers? How would the time complexity change?"
Practice
sum() function do in Python?Solution
Step 1: Understand the purpose of
Thesum()sum()function takes an iterable like a list or tuple and adds all its numbers together.Step 2: Compare options with the function's behavior
Only Adds all numbers in an iterable and returns the total correctly describes adding all numbers and returning the total.Final Answer:
Adds all numbers in an iterable and returns the total -> Option CQuick Check:
sum()adds numbers [OK]
- Confusing sum() with multiplication
- Thinking sum() counts elements
- Assuming sum() finds max value
nums starting from 10?Solution
Step 1: Recall the sum() function signature
The sum() function syntax issum(iterable, start=0), where the second argument is the starting value.Step 2: Identify correct argument order
sum(nums, 10) usessum(nums, 10), which correctly passes the iterable first and start second.Final Answer:
sum(nums, 10) -> Option AQuick Check:
sum(iterable, start) = sum(nums, 10) [OK]
- Swapping the order of arguments
- Using plus sign inside sum()
- Using keyword argument incorrectly
numbers = [2, 4, 6] result = sum(numbers, 5) print(result)
Solution
Step 1: Calculate sum of list elements
The listnumberscontains 2, 4, and 6. Their sum is 2 + 4 + 6 = 12.Step 2: Add the start value
The start value is 5, so total is 12 + 5 = 17.Final Answer:
17 -> Option DQuick Check:
sum([2,4,6],5) = 17 [OK]
- Forgetting to add the start value
- Adding start value before summing
- Assuming sum returns list length
values = [1, 2, '3', 4] total = sum(values)
Solution
Step 1: Check list element types
The listvaluescontains integers and a string '3'. sum() cannot add strings to numbers.Step 2: Understand sum() type requirements
sum() requires all elements to be numbers. Mixing types causes a TypeError.Final Answer:
List contains a string which cannot be added -> Option BQuick Check:
sum() needs all numbers [OK]
- Ignoring mixed data types
- Thinking sum() needs start argument
- Assuming sum() works on strings
sales = [100, 200, 0, 150, 300]. You want to calculate the total sales but ignore days with zero sales. Which code correctly uses sum() to do this?Solution
Step 1: Filter out zero sales using a generator expression
The expressionsale for sale in sales if sale != 0creates a sequence of sales excluding zeros.Step 2: Use sum() on filtered sales
sum() adds only the non-zero sales, giving the correct total.Final Answer:
sum(sale for sale in sales if sale != 0) -> Option AQuick Check:
sum(filtered sales) = correct total [OK]
- Subtracting the count of zeros instead of filtering
- Using sum() with wrong condition
- Dividing sum by zero count
