Python append() — Silent None Broke a Payment Batch
Payment batch empty after assigning return value.
append()
20+ years shipping production Python across data and backend systems. Written from production experience, not tutorials.
- ✓Basic programming fundamentals
- ✓A computer with internet access
- ✓Willingness to follow along with examples
- append() adds one item to the end of a list in place and returns None
- Amortized O(1) — ideal for collecting items one at a time, but not for prepending
- Over-allocation minimizes reallocation; append in a loop is cheap for up to ~10M items
- Production trap: assigning
my_list = my_list.append(x)silently replaces the list with None - Biggest mistake: using append() to merge two lists — produces a nested list, not a flat one
is a list method in Python that adds a single element to the end of a list, modifying the list in place and returning append()None. This return value is the source of countless bugs: developers who expect to return the updated list (like append() in JavaScript or push() in Java) instead get add()None, which silently breaks chained operations, assignment expressions, and data pipelines.
The method exists because Python's design philosophy favors explicit mutation over implicit returns — changes the list directly rather than creating a new one, making it memory-efficient for large collections but treacherous for the unwary.append()
In the Python ecosystem, is the workhorse for building lists incrementally, but it's often confused with append() (which adds each element of an iterable individually) and extend() (which adds at a specific index). Choose insert() when you're adding a single item; use append() to merge lists without nesting; reach for extend() only when order matters and you can afford the O(n) cost.insert()
The real power of emerges in loops — it's the foundation of every data pipeline that collects results, from web scrapers accumulating pages to ETL jobs building record batches. But that same pattern becomes a bug when you append to a list you don't own, mutating shared state across functions or threads.append()
Performance-wise, has amortized O(1) time complexity thanks to Python's over-allocation strategy — lists grow by roughly 12.5% when they hit capacity, so most appends are cheap. But that occasional resize triggers a full memory copy, which can stall real-time systems or cause latency spikes in high-frequency trading applications.append()
For append-heavy workloads where you know the final size, pre-allocate with [None] * n and assign by index to avoid resizes entirely. In multithreaded code, is not atomic — two threads appending simultaneously can corrupt the list's internal state, losing records or crashing.append()
Use queue.Queue or collections.deque with explicit locking instead, or switch to immutable data structures if you need thread safety without locks.
Picture a grocery receipt printing at the checkout. Every time the cashier scans an item, it gets added to the bottom of the receipt — one item at a time, in order, without touching anything already printed. Python's append() does exactly that to a list: it staples one new item onto the end, leaves everything else exactly where it was, and costs you almost nothing in speed. The receipt doesn't reprint itself from scratch. It just grows.
The most common bug I've seen in junior Python code isn't a syntax error — it's a developer calling append() inside a loop and silently building a list of None values for ten thousand iterations because they assigned the return value instead of letting it mutate in place. No exception. No warning. Just wrong data flowing downstream into a database insert at 2am. That's the trap. Learn to see it before it bites you.
Lists are Python's workhorse. You'll use them everywhere — collecting API responses, building queues, assembling rows before a bulk insert, accumulating user events. append() is the single most common way to add something to a list, and it's deceptively simple. 'Deceptively' is the key word. Because its simplicity hides a behaviour — in-place mutation with no return value — that will confuse you at exactly the wrong moment if nobody tells you upfront.
By the end of this, you'll know exactly how append() works under the hood, why it returns None (and what that costs you if you forget), how to use it correctly inside real patterns like event collectors and batch processors, and the three specific mistakes that separate developers who actually know this from developers who just got lucky so far.
What append() Actually Does — and Why None Isn't a Bug
Before you write a single line, you need to understand the contract append() makes with you. It takes the list you already have, tacks one item onto its right end, and modifies that exact list in memory. It does not create a new list. It does not return the updated list. It returns None. Full stop.
Why? Because Python's designers made a deliberate choice: functions that mutate an object in place return None to signal 'I changed the thing you gave me — don't go looking for a new thing.' This is called the Command-Query Separation principle in practice. append() is a command. Commands don't return results — they produce side effects.
This matters because every single time I've paired with a junior developer and seen a silent bug, it traced back to this line: my_list = my_list.append(item). That reassignment just torched their list. The original list got the item added correctly. Then they immediately replaced the variable with None. Every subsequent operation on my_list raises AttributeError: 'NoneType' object has no attribute... — or worse, it silently fails downstream where the None gets serialised and stored. Don't assign the return value. Ever.
# io.thecodeforge — Python tutorial # Real scenario: collecting incoming webhook events before bulk-inserting into a database. # We receive events one at a time and batch them up to reduce DB round-trips. def collect_webhook_events(raw_event_stream): """ Accepts an iterable of raw event dicts from a webhook receiver. Returns a list of validated event payloads ready for bulk insert. """ validated_events = [] # Start with an empty list — our 'receipt' for raw_event in raw_event_stream: # Basic validation — skip anything malformed rather than crashing the whole batch if not isinstance(raw_event, dict): continue if "event_type" not in raw_event or "timestamp" not in raw_event: continue # Build a clean payload — only the fields we actually need clean_payload = { "event_type": raw_event["event_type"], "timestamp": raw_event["timestamp"], "user_id": raw_event.get("user_id", "anonymous"), # default if missing } # append() mutates validated_events IN PLACE and returns None. # Do NOT write: validated_events = validated_events.append(clean_payload) # That would replace your list with None immediately. validated_events.append(clean_payload) return validated_events # --- Simulate incoming webhook data --- incoming_stream = [ {"event_type": "page_view", "timestamp": "2024-01-15T10:00:01Z", "user_id": "usr_001"}, {"event_type": "button_click", "timestamp": "2024-01-15T10:00:03Z", "user_id": "usr_002"}, "this_is_malformed", # Will be skipped by our type check {"event_type": "checkout", "timestamp": "2024-01-15T10:00:07Z"}, # Missing user_id — defaulted {"timestamp": "2024-01-15T10:00:09Z"}, # Missing event_type — will be skipped ] batch = collect_webhook_events(incoming_stream) print(f"Collected {len(batch)} valid events for bulk insert:") for event in batch: print(event) # Prove the return value of append() itself is None proof_list = [1, 2, 3] return_value = proof_list.append(4) print(f"\nappend() returned: {return_value}") # None print(f"But the list is now: {proof_list}") # [1, 2, 3, 4]
my_list = my_list.append(item) silently replaces your entire list with None. You won't get an exception on this line — you'll get AttributeError: 'NoneType' object has no attribute 'append' three lines later when you try to use it again, and you'll spend 20 minutes staring at the wrong line.= .append(, stop and fix it immediately.append(). Amortized O(1).append() vs extend() vs insert() — Pick the Wrong One and You Get Nested Lists
Python gives you three ways to add things to a list and they are not interchangeable. Confuse them and you will silently corrupt your data structure with no error to guide you back.
append() adds exactly one object to the end. That object can be anything — a string, a number, a dict, another list. If you pass it a list, you get a list nested inside your list. Not a merged list. A nested one. I've seen this produce a list like [[1,2,3], [4,5,6]] when the developer expected [1,2,3,4,5,6] — and that data went straight into a JSON column in Postgres looking completely valid until the frontend exploded trying to iterate it.
extend() takes an iterable and adds each of its items individually to the end. This is what you want when you're merging two lists. insert() takes an index and an object, and puts that object at the specified position, shifting everything else right. insert() is O(n) — it has to move every element after the insertion point. append() is amortised O(1). For a list with a million items, that difference is not academic.
# io.thecodeforge — Python tutorial # Scenario: An e-commerce checkout service merges a guest cart (session-based) # with a logged-in user's saved cart when they authenticate. guest_cart_items = ["wireless_mouse", "usb_hub"] saved_cart_items = ["mechanical_keyboard", "monitor_stand", "webcam"] # --- Scenario 1: WRONG — using append() to merge two lists --- # This is the exact mistake that produces nested lists in production wrong_merged_cart = [] wrong_merged_cart.append(guest_cart_items) # Adds the entire list as ONE item wrong_merged_cart.append(saved_cart_items) # Same — another nested list print("WRONG (append to merge lists):") print(wrong_merged_cart) print(f"Item count: {len(wrong_merged_cart)}") # 2 — not 5! print() # --- Scenario 2: CORRECT — using extend() to merge two lists --- correct_merged_cart = [] correct_merged_cart.extend(guest_cart_items) # Adds each item individually correct_merged_cart.extend(saved_cart_items) # Same — now flat, merged list print("CORRECT (extend to merge lists):") print(correct_merged_cart) print(f"Item count: {len(correct_merged_cart)}") # 5 — correct print() # --- Scenario 3: append() is RIGHT when adding a single new item --- # Customer adds one more item to their cart after merging correct_merged_cart.append("laptop_stand") # One item — append is exactly right here print("After adding one more item with append():") print(correct_merged_cart) print() # --- Scenario 4: insert() — when ORDER matters and append() isn't enough --- # A priority item (same-day delivery eligible) must be placed at the front priority_item = "express_delivery" correct_merged_cart.insert(0, priority_item) # index 0 = front of list — O(n) cost print("After insert() at index 0 for priority item:") print(correct_merged_cart) print(f"First item: {correct_merged_cart[0]}") # express_delivery
merged = list_a + list_b. This creates a brand new list and leaves both originals untouched — critical when you're working with shared state across threads or need an audit trail of original carts.append() to combine user history with trending items.append() adds one element; extend() adds many elements flat.append() for single items, extend() for merging iterables.Appending Inside Loops — The Pattern That Powers Real Data Pipelines
The single most common place you'll use append() in production code is inside a loop — transforming, filtering, or enriching a dataset one item at a time before handing it off somewhere else. This pattern is so common it has a name: the accumulator pattern. Master it and you'll use it every day.
The trap here isn't append() itself — it's forgetting that you're mutating a shared list. If you define your accumulator list outside the function and reuse it across calls, you will accumulate state across invocations. I've seen this exact bug in a rate-limiter: the list of blocked IPs was defined at module level, never cleared between requests, and by hour six of production traffic it held thirty thousand stale entries and every lookup was O(n). The service started timing out. Alerts fired. Not a Python bug — a scoping bug made worse by mutation.
Always define your accumulator inside the function unless you explicitly want shared, persistent state. And if you do want persistent state, document it loudly.
# io.thecodeforge — Python tutorial # Scenario: A log ingestion service reads raw log lines from a file, # filters out noise (DEBUG level), enriches each line with a severity score, # and returns a clean batch ready for forwarding to an alerting system. def process_log_batch(raw_log_lines): """ Filters and enriches a batch of raw log strings. Returns only WARNING and above, with a numeric severity attached. The accumulator list is LOCAL — no bleed between calls. """ severity_map = { "DEBUG": 1, "INFO": 2, "WARNING": 3, "ERROR": 4, "CRITICAL": 5, } # Accumulator defined INSIDE the function — resets to empty on every call. # Defining this outside the function is the classic shared-state trap. processed_entries = [] for raw_line in raw_log_lines: # Guard: skip blank lines or anything that isn't a string if not isinstance(raw_line, str) or not raw_line.strip(): continue parts = raw_line.strip().split(" ", 2) # Split into max 3 parts: timestamp, level, message if len(parts) < 3: continue # Malformed line — skip rather than crash timestamp, level, message = parts # Only forward WARNING and above to the alerting system if level not in severity_map or severity_map[level] < 3: continue enriched_entry = { "timestamp": timestamp, "level": level, "message": message, "severity_score": severity_map[level], # Numeric score for downstream sorting } # append() adds this one enriched dict to the end of our accumulator processed_entries.append(enriched_entry) # Sort by severity descending so CRITICAL bubbles to the top of the alert queue processed_entries.sort(key=lambda entry: entry["severity_score"], reverse=True) return processed_entries # --- Simulate a raw log batch from a web server --- raw_logs = [ "2024-01-15T10:00:01Z DEBUG Health check passed", "2024-01-15T10:00:03Z INFO User usr_042 logged in", "2024-01-15T10:00:05Z WARNING Database connection pool at 80% capacity", "2024-01-15T10:00:06Z ERROR Payment gateway timeout after 30s", "2024-01-15T10:00:07Z DEBUG Cache hit ratio: 0.94", "2024-01-15T10:00:08Z CRITICAL Disk usage at 99% on /var/log — writes failing", "", # Blank line — will be skipped "malformed_no_spaces", # Malformed — will be skipped ] alerts = process_log_batch(raw_logs) print(f"Forwarding {len(alerts)} alerts to alerting system (sorted by severity):\n") for alert in alerts: print(f"[{alert['severity_score']}] {alert['level']:8s} | {alert['timestamp']} | {alert['message']}")
Appending to Lists You Don't Own — Mutation, Copies, and When append() Becomes a Bug
append() mutates the list in place. That's its entire value proposition. But mutation becomes a liability the moment your list is shared — passed into a function, stored as a default argument, or referenced from multiple variables. This is where beginners get hurt in ways that feel like black magic.
The most notorious version of this is Python's mutable default argument trap. If you write def add_item(item, collection=[]), that empty list [] is created exactly once when the function is defined — not each time it's called. Every call that uses the default shares the same list. Your third call to that function will have items from the first two calls sitting in collection. I've seen this quietly corrupt a recommendation engine's candidate list across user sessions in production. The fix is always the same: use None as the default and initialise inside the function.
The second version is reference aliasing: cart_a = cart_b. That doesn't copy the list. Both variables now point to the same list in memory. Appending to cart_a modifies cart_b too. If you need an independent copy, use cart_a = for a shallow copy, or cart_b.copy()copy.deepcopy(cart_b) if the list contains nested mutable objects you also need to isolate.
# io.thecodeforge — Python tutorial import copy # For deep copying nested structures # ============================================================ # TRAP 1: Mutable default argument — the most infamous Python gotcha # ============================================================ # WRONG: default argument [] is created ONCE at function definition time def add_to_order_broken(item, order_items=[]): order_items.append(item) return order_items print("=== Mutable Default Argument Trap ===") order_one = add_to_order_broken("coffee") order_two = add_to_order_broken("muffin") # Should start fresh — but it won't print(f"Order 1 (expected: ['coffee']) : {order_one}") # ['coffee', 'muffin'] — WRONG print(f"Order 2 (expected: ['muffin']) : {order_two}") # ['coffee', 'muffin'] — same object! print() # CORRECT: use None as sentinel, initialise inside the function def add_to_order_correct(item, order_items=None): if order_items is None: order_items = [] # Fresh list on every call that doesn't pass one in order_items.append(item) return order_items order_three = add_to_order_correct("coffee") order_four = add_to_order_correct("muffin") print("=== Fixed Version ===") print(f"Order 3 (expected: ['coffee']) : {order_three}") # ['coffee'] — correct print(f"Order 4 (expected: ['muffin']) : {order_four}") print() # ============================================================ # TRAP 2: Reference aliasing — two names, one list # ============================================================ print("=== Reference Aliasing Trap ===") user_a_cart = ["laptop", "mouse"] user_b_cart = user_a_cart # NOT a copy — both point to the same list in memory user_b_cart.append("keyboard") # Intending to only modify user B's cart print(f"User A cart (expected unchanged): {user_a_cart}") # ['laptop', 'mouse', 'keyboard'] — WRONG print(f"User B cart : {user_b_cart}") # ['laptop', 'mouse', 'keyboard'] print() # CORRECT: shallow copy for a flat list user_c_cart = ["laptop", "mouse"] user_d_cart = user_c_cart.copy() # Independent copy of the top-level list user_d_cart.append("keyboard") print("=== Fixed with .copy() ===") print(f"User C cart (untouched) : {user_c_cart}") # ['laptop', 'mouse'] — correct print(f"User D cart : {user_d_cart}") # ['laptop', 'mouse', 'keyboard'] print() # CORRECT: deep copy when list contains nested mutable objects (e.g. dicts) user_e_cart = [{"sku": "laptop", "qty": 1}, {"sku": "mouse", "qty": 2}] user_f_cart = copy.deepcopy(user_e_cart) # Full independent clone, including nested dicts user_f_cart[0]["qty"] = 99 # Change only user F's quantity print("=== Deep Copy for Nested Objects ===") print(f"User E laptop qty (untouched): {user_e_cart[0]['qty']}") # 1 — correct print(f"User F laptop qty : {user_f_cart[0]['qty']}") # 99
[] or {} as a default function argument is one of Python's most infamous gotchas. The list is created once at function definition time and shared across every call. The symptom is data bleeding between function calls with no obvious cause. The fix is always def fn(items=None) with if items is None: items = [] inside the body.append() changes every reference to that list.Performance Characteristics of append(): Amortized Cost and When Not to Use It
You've seen how to use append() correctly. Now understand the cost and the edge cases where it becomes a bottleneck.
append() is amortized O(1). That means most calls are constant time, but occasionally a call triggers a resize that costs O(n) — copying the entire existing list to a larger underlying array. The key is the 'over-allocation' strategy: Python's list implementation allocates extra capacity (≈12.5% extra) so that many subsequent appends happen without a reallocation. For most workloads this is excellent: appending 10 million items one by one takes under a second in CPython.
But there are three situations where append() is the wrong tool:
- Prepending to the front: If you need to add items at index 0, don't use insert(0, item) or append in reverse. insert(0) is O(n) every time. For a queue, use
collections.dequewhich offers O(1)appendleft()andpopleft(). - Building a list where you know the final size in advance: If you know you'll collect exactly N items, preallocate with
[None] Nand assign by index. This avoids reallocation overhead entirely. Example:results = [None] N; for i, val in enumerate(source): results[i] = transform(val). - Real-time or latency-sensitive systems: An amortized O(1) operation still has worst-case O(n) resizes. For applications that cannot tolerate occasional latency spikes, use a linked-list structure or preallocate. In practice, this matters only at very high frequencies (millions of appends per second) or when every microsecond counts.
# io.thecodeforge — Python tutorial import time from collections import deque # Scenario: Build a list of 1 million integers using different strategies N = 1_000_000 # --- Strategy 1: append() in loop (the common pattern) --- start = time.perf_counter() result = [] for i in range(N): result.append(i) append_time = time.perf_counter() - start print(f"append() loop: {append_time:.3f}s") # --- Strategy 2: Preallocate with [None]*N and assign --- start = time.perf_counter() result = [None] * N for i in range(N): result[i] = i prealloc_time = time.perf_counter() - start print(f"Preallocate + assign: {prealloc_time:.3f}s") # --- Strategy 3: list comprehension (most Pythonic) --- start = time.perf_counter() result = [i for i in range(N)] lc_time = time.perf_counter() - start print(f"List comprehension: {lc_time:.3f}s") # --- Strategy 4: insert(0, item) for building a queue (the wrong way) --- start = time.perf_counter() result = [] for i in range(N): result.insert(0, i) insert_time = time.perf_counter() - start print(f"insert(0, i) loop: {insert_time:.3f}s (avoid! ~ O(n^2))") # --- Strategy 5: deque appendleft for building a queue --- start = time.perf_counter() d = deque() for i in range(N): d.appendleft(i) deque_time = time.perf_counter() - start print(f"deque appendleft: {deque_time:.3f}s") print() print(f"append() vs preallocate ratio: {append_time / prealloc_time:.2f}x") print(f"insert(0) vs deque ratio: {insert_time / deque_time:.2f}x — always use deque for left-side adds")
append() inside a real-time data feed handler.Append in Multithreaded Code — Why Your List Just Lost a Million Records
Appending to a plain Python list from multiple threads looks safe. It's not. CPython's GIL gives you the illusion of safety, but it only protects individual bytecode instructions — not the whole append operation. When two threads try to resize the underlying C array at the same time, you get silent data loss or a corrupted internal pointer.
This isn't theoretical. I've debugged production systems where event collectors lost thousands of records overnight. The list looked fine, lengths matched, but records vanished. The root cause? Thread A triggered a reallocation while Thread B wrote to stale memory. The GIL didn't help because the resize and the pointer update weren't atomic.
Use a queue.Queue or collections.deque if you need thread-safe appends. They use proper locks. If throughput matters, consider a lock-free buffer from the multiprocessing module. Never assume list.append is safe just because Python feels high-level.
// io.thecodeforge — python tutorial import threading import time shared_list = [] APPEND_COUNT = 100_000 def unsafe_appender(): for i in range(APPEND_COUNT): shared_list.append(i) # Two threads can trigger resize simultaneously t1 = threading.Thread(target=unsafe_appender) t2 = threading.Thread(target=unsafe_appender) t1.start() t2.start() t1.join() t2.join() print(f"Expected length: {2 * APPEND_COUNT}") print(f"Actual length: {len(shared_list)}") print(f"Lost records: {2 * APPEND_COUNT - len(shared_list)}")
Append With Generators — Load Data in Chunks Without Blowing Memory
You're building a pipeline that processes a million CSV rows. Loading everything into a list with .append() works until your machine hits swap. The fix isn't to stop using append — it's to pair it with generators so you never hold the full set in memory.
Generators yield one item at a time. Your processing function can append to a small batch list, then flush it to a database or file. You get the simplicity of list.append without the memory cost. This is how real ETL pipelines work: a generator streams records, and append builds tiny buffers that get consumed immediately.
Typical pattern: a generator reads from a socket or file, your transformer processes each record, and you append results to a fixed-size list. Once it hits your batch limit (say 500), you write the batch and clear the list. Append still works — you just control how much accumulates.
// io.thecodeforge — python tutorial from typing import Generator, List import json def record_generator() -> Generator[dict, None, None]: """Simulates streaming data — never creates a full list.""" for i in range(1_000_000): yield {"id": i, "value": i * 2} def flush_batch(batch: List[dict]) -> None: """Write batch to disk or database. In production, this would be a PostgreSQL INSERT or file write.""" print(f"Flushed {len(batch)} records") batch_capacity = 500 current_batch = [] for record in record_generator(): current_batch.append(record) # Accumulates one at a time if len(current_batch) >= batch_capacity: flush_batch(current_batch) current_batch.clear() # Release memory, reuse the list object # Flush the last partial batch if current_batch: flush_batch(current_batch)
list.clear() instead of assigning a new list (current_batch = []) to reuse the same memory allocation — avoids reallocating the list's PyObject header.Creating Stacks and Queues With Python's .append()
Lists are the Swiss Army knife of Python collections, but they make lousy queues. When you pop from the front of a list, every single element shifts left — that's O(n) work for every pop. Your production pipeline just turned into a parking lot. Stacks, on the other hand, are where list.append() shines. Push on the right, pop off the right — both O(1) amortized. You get a perfect LIFO structure with zero overhead.
A proper queue requires collections.deque. Use deque.append() for the right side and deque.appendleft() for the left. Both are O(1). Never simulate a queue with a list unless you enjoy explaining latency spikes to your manager. The deque also has a maxlen parameter — bind it and you get an automatic sliding window. That's the difference between hobby code and production systems.
When you need LIFO, grab a list. When you need FIFO, grab a deque. The wrong choice costs you orders of magnitude in performance.
// io.thecodeforge — python tutorial from collections import deque # Stack: list.append() + list.pop() task_stack = [] task_stack.append('deploy') task_stack.append('test') task_stack.append('build') print(task_stack.pop()) # 'build' print(task_stack.pop()) # 'test' print(task_stack.pop()) # 'deploy' # Queue: deque.append() + deque.popleft() job_queue = deque() job_queue.append('process_logs') job_queue.append('send_alert') job_queue.append('reindex') print(job_queue.popleft()) # 'process_logs' print(job_queue.popleft()) # 'send_alert' print(job_queue.popleft()) # 'reindex' # Sliding window with maxlen recent_errors = deque(maxlen=3) for error in ['e1', 'e2', 'e3', 'e4']: recent_errors.append(error) print(list(recent_errors)) # ['e2', 'e3', 'e4']
deque.appendleft() and deque.popleft() for constant-time operations.list.append() for stacks (LIFO) and deque.append()/deque.popleft() for queues (FIFO). The wrong choice creates O(n) bottlenecks.array.append() — When You Need C-Level Speed Without NumPy
Python lists store pointers to objects. That's fine for mixed types, but when you're processing millions of integers or floats, those pointer indirections add cache misses. The array module gives you type-specific sequences — think of it as a thin C array wrapped in Python syntax. Every element is a raw C value packed sequentially in memory. Less overhead, better cache locality, faster iteration.
Call array.append() exactly like list.append(). The difference is you must declare the type code when creating the array: 'i' for signed int, 'f' for float, 'd' for double. That type constraint means append rejects wrong types at insertion time — not at some random point three hours into a batch job. You find bugs early.
Performance gain? About 2-5x faster iteration and 75% less memory for homogeneous numeric data. When you can't justify NumPy's dependency weight but need to process a CSV with ten million integers, array.append() is your answer. It's Python's production-ready secret weapon for memory-bound pipelines.
// io.thecodeforge — python tutorial from array import array # Signed int array — append like a list sensor_readings = array('i') for reading in [23, 45, 67, 89]: sensor_readings.append(reading) # Each element is 4 bytes (C int) vs 28 bytes for Python int object print(sensor_readings) # array('i', [23, 45, 67, 89]) print(sensor_readings.buffer_info()[1] * 4, 'bytes') # 16 bytes total # Type enforcement catches bugs early try: sensor_readings.append(3.14) except TypeError as e: print(f'Rejected: {e}') # Rejected: integer required # Float array — use 'f' for 4-byte, 'd' for 8-byte prices = array('d', [99.95, 49.99]) prices.append(29.99) print(prices) # array('d', [99.95, 49.99, 29.99])
Switching Back to .append() — When You Inherit Legacy Anti-Patterns
You join a team. You see list concatenation everywhere: full_list = full_list + new_items. That creates a new list object every iteration — O(n) allocation per operation, quadratic runtime. The original author probably read some Java tutorial from 2005 and never questioned it. Your job is to swap that garbage for .append() and walk away.
Then there's the += crowd. On lists, list_a += list_b is equivalent to .extend(), not .append(). It mutates in place but still builds a temporary tuple internally. Worse, it's syntactically ambiguous. A junior writes result += [item] and expects item appended — but extend flattens the list one level. That's a nested-list bug waiting to ship.
Switch back to explicit .append() for single elements. It's clearer, faster, and doesn't generate garbage objects for the GC to collect. When you see += [item] in a code review, flag it. When you see list = list + [item] in a loop, rewrite it. Your future self — and your CPU — will thank you.
// io.thecodeforge — python tutorial import timeit # Anti-pattern: list = list + [item] — O(n) per append bad_list = [] for i in range(1000): bad_list = bad_list + [i] # creates new list each time # Proper: .append() — amortized O(1) good_list = [] for i in range(1000): good_list.append(i) # Also wrong: += [item] — ambiguous, flattens data = [1, 2] data += [3] # [1, 2, 3] — works but misleading data = [1, 2] data.extend([3]) # explicit — same result # Performance comparison bad_time = timeit.timeit( 'lst = []; [lst := lst + [x] for x in range(100)]', number=1000 ) good_time = timeit.timeit( 'lst = []; [lst.append(x) for x in range(100)]', number=1000 ) print(f'List concat: {bad_time:.3f}s') # ~0.2s print(f'Append: {good_time:.3f}s') # ~0.01s
list = list + [item] in loops. Ban += [item] for single-element appends. Require explicit .append() or .extend() — readability wins and garbage collection loses.The Silent None: How Assigning append() Return Value Corrupted a Payment Batch
append() returns the updated list, like some other languages' push methods. The code batch = batch.append(record) looked natural because it followed the pattern of immutable operations.list.append() mutates the list in place and returns None. The assignment batch = ... overwrote the list variable with None on the first iteration. Subsequent iterations tried to call batch.append(record) on None, raising AttributeError inside the loop — but the exception was caught by a generic except: clause that logged nothing and continued.batch = batch.append(record) to batch.append(record). The list is already updated. Also remove the bare except: and replace it with specific exception handlers that log and escalate.- Never assign the return value of
append()— it's always None. - Bare except: clauses are dangerous — they swallow exceptions, including the AttributeError that would have revealed the bug immediately.
- Always validate that an accumulation loop produces the expected item count. A len(batch) check after the loop would have caught the zero-length result.
= .append( in the codebase. The assignment is overwriting the list with None. Change to just .append(). Also check for bare except clauses that might be swallowing the AttributeError.append() was used to combine two lists. Replace with extend() for flat merging. Example: result.extend(other_list) instead of result.append(other_list).def fn(items=[]). Replace with def fn(items=None) and initialize items = [] inside the function body.collections.deque(maxlen=N).grep -n '= .*\.append(' *.pypython -c "import ast; ast.parse(open('file.py').read())" (syntax check won't catch it — grep is the tool)x = x.append(y) to x.append(y).grep -n '\.append\(' *.pyReview each append inside loops that combine data sourcesresult.append(other_list) with result.extend(other_list) for flat merging.grep -n 'def .*=\[\]' *.pypython -c "from inspect import signature; print(signature(your_module.your_function))"def fn(items=[]) to def fn(items=None) and add if items is None: items = [].| Method | What It Adds | Mutates Original? | Returns | Time Complexity | Use When |
|---|---|---|---|---|---|
| append(item) | Exactly one object (any type) | Yes | None | Amortised O(1) | Adding a single item — a new event, a parsed row, one API result |
| extend(iterable) | Each item from an iterable individually | Yes | None | O(k) where k = len of iterable | Merging two lists flat — combining two result sets without nesting |
| insert(index, item) | Exactly one object at a specific position | Yes | None | O(n) — shifts all elements after index | Order matters and the item must go somewhere other than the end |
| list + list | Each item from second list individually | No — creates new list | New list | O(n+m) | You need a merged list without touching the originals |
| list comprehension | Transformed/filtered items from iterable | No — creates new list | New list | O(n) | You're transforming while collecting — cleaner than append in a loop |
| File | Command / Code | Purpose |
|---|---|---|
| EventCollector.py | def collect_webhook_events(raw_event_stream): | What append() Actually Does |
| ShoppingCartMerge.py | guest_cart_items = ["wireless_mouse", "usb_hub"] | append() vs extend() vs insert() |
| LogLineProcessor.py | def process_log_batch(raw_log_lines): | Appending Inside Loops |
| UserSessionCart.py | def add_to_order_broken(item, order_items=[]): | Appending to Lists You Don't Own |
| PerformanceComparison.py | from collections import deque | Performance Characteristics of append() |
| ThreadedAppendLoss.py | shared_list = [] | Append in Multithreaded Code |
| BatchedAppendPipeline.py | from typing import Generator, List | Append With Generators |
| stack_queue_demo.py | from collections import deque | Creating Stacks and Queues With Python's .append() |
| array_append_demo.py | from array import array | array.append() |
| fix_bad_append.py | bad_list = [] | Switching Back to .append() |
Key takeaways
my_list = my_list.append(x) you've destroyed your list. Call it as a standalone statement and walk away.len() reporting 1 when you expected 5, with no error to guide you. Use extend() to merge.append() when you're collecting items one at a time inside a loopappend() changes the list every variable pointing to that object can see. If you pass a list into a function and append inside it, the caller's list changes too. Copy first if you need isolation.Common mistakes to avoid
4 patternsAssigning the return value of append()
AttributeError: 'NoneType' object has no attribute...append(). Call my_list.append(item) as a standalone statement.Using append() to merge two lists
[[1,2], [3,4]]) instead of a flat list ([1,2,3,4]). len() reports the number of original lists, not total items.extend() to add all items from an iterable individually: result.extend(other_list).Using a mutable list as a default function argument
None as the default and initialize inside the function: def fn(items=None): if items is None: items = [].Aliasing a list instead of copying before appending
copy() for flat lists or copy.deepcopy() for nested mutable objects before appending if you need an independent copy.Interview Questions on This Topic
Python lists are dynamic arrays under the hood. When you call append() repeatedly in a loop, Python doesn't allocate memory for every single item — it over-allocates in chunks. What are the performance implications of this for very large lists, and at what point would you stop using a list with append() in favour of a different data structure like collections.deque or a pre-allocated array?
append() amortized O(1) time but occasional O(n) resizes. For most workloads this is fine — appending 10 million integers takes ~0.1s in CPython. However, there are three cases where you should reconsider:
1. Prepending: If you need O(1) left-side additions, use collections.deque with appendleft().
2. Known final size: If you know the exact N items, preallocate with [None] * N and assign by index — avoids reallocation overhead.
3. Latency-sensitive systems: If sporadic resize delays are unacceptable (e.g., real-time trading), preallocate or use a data structure with guaranteed O(1) per operation.
For extremely large lists (>10 million items), memory fragmentation becomes a concern. Consider using array.array('i') for typed data or a database for persistence.You're building a high-throughput event ingestion service where multiple threads are simultaneously calling append() on a shared list. Is Python's list.append() thread-safe, and what's your strategy for collecting events from concurrent producers without data corruption or race conditions?
if len(events) < max: events.append(item)), the GIL can be released between the check and the append, leading to race conditions.
For true concurrent producers, use queue.Queue (fully thread-safe, designed for producer-consumer) or collections.deque with manual locks. For high-throughput scenarios, consider using asyncio with a single event loop and awaitable queues, which avoids threading complexity entirely.A colleague's code builds a result list using append() inside a nested loop and then passes it as a default argument to another function. Without running the code, what are the two distinct bugs in that design, what are the exact symptoms you'd see at runtime, and how do you fix both?
results=[] will share the same list across all calls. Symptoms: data bleeding between calls — results accumulate across invocations.
Bug 2: Shared mutation via reference aliasing. The inner function appends to a list that is also referenced elsewhere. Symptoms: modifying the list in one place silently changes it in another.
Fixes:
1. Change the default argument to None and initialize inside the function.
2. If the caller needs an independent copy, use .copy() before passing the list to the function, or have the function return a new list instead of mutating the input.Frequently Asked Questions
It's a deliberate design decision called Command-Query Separation: functions that mutate an object return None to signal that the change happened in place — no new object was created. This prevents you from accidentally chaining operations on a new copy that doesn't exist. The list you passed in is the list that changed — go use that one.
append() adds one object to the end of a list — if that object is another list, you get a nested list. extend() unpacks an iterable and adds each item individually, producing a flat merged list. The rule: if you want to add a single thing, use append(); if you want to merge two lists without nesting, use extend().
Use extend() with an iterable: my_list.extend([item1, item2, item3]). This adds each item individually in O(k) time where k is the number of new items. Alternatively, the += operator on a list calls extend() under the hood: my_list += [item1, item2, item3] produces the same result. Don't call append() in a loop when extend() does it in one call.
Technically, CPython's GIL makes append() itself atomic for a single call, so you won't corrupt the internal array structure with concurrent appends. But 'GIL-atomic' is not the same as 'logically safe' — if your code does a read-check-then-append pattern (e.g. checking length before appending), another thread can execute between your check and your append, giving you race conditions in logic even without memory corruption. For true concurrent producers, use collections.deque with its thread-safe appendleft()/append(), or a queue.Queue, which was explicitly designed for producer-consumer patterns across threads.
Preallocate when you know the exact final number of items and you're in a latency-sensitive or performance-critical path. Preallocation with [None] * N and index assignment avoids the occasional O(n) resize overhead. For most everyday code, the performance difference is negligible — use append() for readability.
20+ years shipping production Python across data and backend systems. Written from production experience, not tutorials.
That's Python Basics. Mark it forged?
9 min read · try the examples if you haven't