๐ Python Mistakes Everyone Makes ❌
Day 7: Using list.sort() Incorrectly
Sorting lists in Python looks simple, but there’s a subtle behavior that often confuses beginners.
❌ The Mistake
❌ Why this fails?
Because list.sort() sorts the list in place and returns None.
-
The original list gets sorted
-
No new list is returned
So sorted_numbers becomes None.
✅ The Correct Way
This modifies the original list and works as expected.
๐ง Simple Rule to Remember
list.sort() → sorts in place, returns None
sorted() → returns a new sorted list
Example:
✅ Key Takeaway
If you need a new sorted list, use sorted().
If you want to modify the existing list, use list.sort().
%20incorrectly.png)

0 Comments:
Post a Comment