Overwrite vs append behavior in Python - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
We want to see how the time it takes to add data changes when we either replace old data or add new data.
How does the way we add data affect how long the program runs?
Analyze the time complexity of the following code snippet.
data = []
for i in range(n):
# Overwrite behavior
data = [i]
for i in range(n):
# Append behavior
data.append(i)
This code first replaces the whole list with a new single-item list each time, then adds items one by one to the list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The first loop overwrites the list each time, the second loop appends items to the list.
- How many times: Both loops run n times, but the cost inside differs.
When overwriting, each step replaces the list quickly, so time grows slowly. When appending, each step adds one item, making the list bigger and bigger.
| Input Size (n) | Approx. Operations Overwrite | Approx. Operations Append |
|---|---|---|
| 10 | 10 simple replacements | 10 appends, list grows to 10 |
| 100 | 100 simple replacements | 100 appends, list grows to 100 |
| 1000 | 1000 simple replacements | 1000 appends, list grows to 1000 |
Pattern observation: Overwrite time grows linearly but stays simple each time; append time also grows linearly but the list size grows, affecting memory.
Time Complexity: O(n)
This means the time to run grows in a straight line as the input size grows, whether overwriting or appending.
[X] Wrong: "Appending is always slower than overwriting because the list gets bigger."
[OK] Correct: Each append is quick and simple, so total time grows evenly. Overwriting replaces the whole list each time but still takes similar total time.
Understanding how different ways of adding data affect time helps you explain your choices clearly and shows you think about program speed in real situations.
"What if we changed the overwrite to create a new list with all previous items plus the new one each time? How would the time complexity change?"
Practice
'w' and write data to it?Solution
Step 1: Understand file mode 'w'
Opening a file with mode 'w' means write mode, which clears existing content.Step 2: Effect of writing in 'w' mode
Writing data in 'w' mode overwrites any existing content with the new data.Final Answer:
The file content is replaced with the new data. -> Option AQuick Check:
Overwrite = Replace content [OK]
- Thinking 'w' appends data
- Confusing 'w' with 'a' mode
- Assuming 'w' opens file for reading
Solution
Step 1: Identify append mode
Mode 'a' opens the file for appending, adding data at the end without deleting existing content.Step 2: Check other modes
'w' overwrites, 'r' reads only, 'x' creates new file and errors if exists.Final Answer:
open('file.txt', 'a') -> Option CQuick Check:
Append mode = 'a' [OK]
- Using 'w' when append is needed
- Confusing 'r' with append mode
- Using 'x' which fails if file exists
lines = ['one', 'two']
with open('test.txt', 'w') as f:
for line in lines:
f.write(line + '\n')
with open('test.txt', 'a') as f:
f.write('three\n')
with open('test.txt') as f:
print(f.read())Solution
Step 1: Write lines with 'w' mode
The first block writes 'one' and 'two' each on new lines, overwriting any old content.Step 2: Append 'three' with 'a' mode
The second block adds 'three' on a new line at the end without removing previous lines.Step 3: Read and print file content
The last block reads all lines, so output is 'one\ntwo\nthree\n'.Final Answer:
one\ntwo\nthree\n -> Option DQuick Check:
Write 'w' then append 'a' = combined content [OK]
- Assuming 'a' overwrites content
- Forgetting newline characters
- Expecting only last write to appear
with open('data.txt', 'w') as f:
f.write('Hello\n')
with open('data.txt', 'w') as f:
f.write('World\n')What is the problem?
Solution
Step 1: Analyze first write
The first block writes 'Hello' and creates or overwrites the file.Step 2: Analyze second write with 'w'
The second block opens the file again in 'w' mode, which clears previous content, then writes 'World'.Final Answer:
The second write overwrites the first line. -> Option AQuick Check:
Opening with 'w' overwrites content [OK]
- Thinking file appends automatically
- Believing file must be closed manually
- Assuming write() can only be called once
Solution
Step 1: Understand log file needs
Logs should be added without deleting old entries, so appending is needed.Step 2: Choose correct mode
Mode 'a' appends data; 'w' overwrites, 'r' is read-only, 'x' creates new file and errors if exists.Final Answer:
with open('log.txt', 'a') as f: f.write('New event\n') -> Option BQuick Check:
Append mode 'a' preserves old data [OK]
- Using 'w' and losing old logs
- Trying to write in 'r' mode
- Using 'x' which fails if file exists
