Step-by-step Explanation
Line 1
lst = [1, 2, 3]You create a list:
lst → [1, 2, 3]Line 2
lst = lst.append(4)This is the key tricky part.
lst.append(4) modifies the list in-place
-
But append() returns None
So this line becomes:
lst = NoneBecause:
lst.append(4) → NoneThe list is updated internally, but you overwrite lst with None.
Line 3
print(lst)Since lst is now None, it prints:
None✅ Final Output
NoneWhy this happens
| Function | Modifies list | Returns value |
|---|---|---|
| append() | Yes | None |
So you should not assign it back.
✅ Correct way to do it
Output:
[1, 2, 3, 4]Summary
append() changes the list but returns None
Writing lst = lst.append(4) replaces your list with None
-
Always use lst.append(...) without assignment


0 Comments:
Post a Comment