sort() In-Place vs sorted() New List

Easy ⏱ 8 min 82% acceptance ★★★★★ 4.8
Write compare_sort_methods(items) that returns a tuple (new_sorted, original_after). new_sorted is the result of calling sorted() on items (which must NOT mutate the input). original_after is the state of items right after calling .sort() on it in place. This demonstrates the difference between the two.

Examples

Example 1
Input
items = [3, 1, 2]
Output
([1, 2, 3], [1, 2, 3])
Explanation

sorted() returns a new list without touching items; then .sort() mutates items in place — both end up sorted, but via different mechanisms.

Constraints

  • 0 <= len(items) <= 10^4

Topics

ListsSorting

Companies

AdobeSalesforce

Hints

Hint 1

Call sorted(items) first and store it.

Hint 2

Then call items.sort() which mutates items directly.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.