@@ -48,11 +48,11 @@ total = sum([x for x in range(1000000)]) # O(n) time, O(n) space
4848### Counting Items
4949
5050``` python
51- # O(n) - count 1s
51+ # O(n) time, O(n) space - list comprehension
5252count = sum ([1 for x in range (100 ) if x % 2 == 0 ]) # 50 even numbers
5353
54- # Better: use len() or sum with boolean
55- count = sum (1 for x in range (100 ) if x % 2 == 0 ) # Still O(n)
54+ # Better: sum with boolean (True=1, False=0) - O(n) time, O(1) space
55+ count = sum (x % 2 == 0 for x in range (100 )) # 50 even numbers
5656```
5757
5858### Computing Statistics
@@ -70,17 +70,17 @@ average = sum(numbers) / len(numbers) # Iterate twice
7070### Flattening Lists
7171
7272``` python
73- # O(n² ) - inefficient!
74- # Each sum call is O(n), and we have n lists
73+ # O(n*k ) - inefficient! n=number of lists, k=items per list
74+ # Each + creates a new list and copies all accumulated items
7575nested = [[1 , 2 ], [3 , 4 ], [5 , 6 ]]
76- flat = sum (nested, []) # O(n² ) - avoid this!
76+ flat = sum (nested, []) # O(n*k ) - avoid this!
7777
7878# Better approaches:
79- # O(n) - single iteration
79+ # O(n*k) total items, but no repeated copying
8080from itertools import chain
8181flat = list (chain(* nested))
8282
83- # O(n) - unpacking
83+ # O(n*k ) - single pass, no repeated copying
8484flat = [item for sublist in nested for item in sublist]
8585```
8686
@@ -174,40 +174,6 @@ total = sum(numbers) # 15
174174totals = list (accumulate(numbers)) # [1, 3, 6, 10, 15]
175175```
176176
177- ## Pitfalls
178-
179- ### Floating Point Precision
180-
181- ``` python
182- # Summing floats can lose precision
183- numbers = [0.1 ] * 10
184- print (sum (numbers)) # 1.0000000000000007, not exactly 1.0
185-
186- # Better: use decimal for financial calculations
187- from decimal import Decimal
188- numbers = [Decimal(' 0.1' )] * 10
189- print (sum (numbers, Decimal(' 0' ))) # Exact: 1.0
190- ```
191-
192- ### Empty Sequence
193-
194- ``` python
195- # sum() of empty is 0
196- result = sum ([]) # 0
197-
198- # Or start value
199- result = sum ([], start = 100 ) # 100
200-
201- # But TypeError with strings if no start
202- try :
203- sum ([" a" , " b" ]) # TypeError
204- except TypeError :
205- pass
206-
207- # Works with start=""
208- result = sum ([" a" , " b" ], start = " " ) # "ab"
209- ```
210-
211177## Performance Notes
212178
213179``` python
@@ -233,15 +199,11 @@ fsum([0.1] * 10) # More precise for floats
233199
234200- Use ` sum() ` for numeric aggregation
235201- Use generator expressions with ` sum() ` for memory efficiency
236- - Use ` sum([], start=[]) ` with explicit start value
237- - Use ` math.fsum() ` when precision matters
238202
239203❌ ** Avoid** :
240204
241205- ` sum(nested_lists, []) ` - O(n²) concatenation
242206- ` sum(strings, "") ` - inefficient string concatenation
243- - Forgetting start value (causes TypeError with non-numeric sequences)
244- - Summing booleans when ` any() ` or ` all() ` is clearer
245207
246208## Related Functions
247209
0 commit comments