๐Python Mistakes Everyone Makes ❌
Day 14: Confusing append() vs extend()
append() and extend() may look similar, but they behave very differently.
❌ The Mistake
Output:
[1, 2, 3, [4, 5]]The entire list is added as a single element.
✅ The Correct Way
Output:
[1, 2, 3, 4, 5]Each element is added individually.
❌ Why This Fails?
append() adds the whole object as one item.
It does not unpack or loop through the elements.
✔ Key Differences
append() → adds one element
extend() → adds multiple elements
๐ง Simple Rule to Remember
append() keeps the list nested
extend() flattens the iterable
%20vs%20extend().png)

0 Comments:
Post a Comment