Concepts / Method vs. Function: When to Use Each

Method vs. Function: When to Use Each

Tuples are immutable and therefore do not provide .sort() or .reverse() methods. These methods modify sequences in place, which violates immutability.

  • Programming

The Tuple Surprise

Lists and tuples are both sequences that can hold multiple items, so it is natural to expect them to offer the same operations. However, a tuple cannot provide sort() or reverse() methods. Those methods modify a sequence in place, while tuples are immutable. In other words, a tuple cannot be changed after it has been created.

What do you think happens?

You have a tuple containing 3, 1, and 2. What should you use if you want its items in ascending order without changing the tuple?

  • Call the tuple's sort() method
  • Call sorted() with the tuple
  • Call the tuple's reverse() method
  • Change the tuple in place
Reveal answer

Answer: Call sorted() with the tuple

Tuples do not provide in-place sorting or reversing methods because those operations would violate immutability. sorted() works with tuples and returns a new list in ascending order.

In-Place Changes

A method such as sort() or reverse() is associated with a sequence object and changes that list itself. This is called modifying the sequence in place. After the operation, the original list has a different order. This approach is appropriate when you are certain that the original order is no longer needed.

sort() changes the listsorted() creates a resultitems[3, 1, 2]items[3, 1, 2]items[1, 2, 3]sorted items[1, 2, 3]
What changes in the original list when a list method is used compared with a built-in function?
python
Output
[1, 2, 3]

The important observation is that numbers is now in ascending order. The list itself was changed. This is different from asking a function to produce another sequence while leaving the original untouched.

Functions That Preserve Data

Python provides the built-in functions sorted() and reversed() for sequence operations that do not modify the original. They work with lists, tuples, strings, and custom sequence types. sorted() returns a new list in ascending order. reversed() returns an iterator that yields the elements in reverse order one at a time.

inputreturnsinputyieldsoriginal sequence[3, 1, 2]sorted()new listsorted result[1, 2, 3]reversed()iteratorreverse-orderelements2, 1, 3
How does the original sequence relate to the object produced by sorted() or reversed()?

numbers = [3, 1, 2] sorted_numbers = sorted(numbers) reversed_numbers = list(reversed(numbers)) print(numbers) print(sorted_numbers) print(reversed_numbers)

Why Tuples Need Functions

providesprovidesworks withworks withlistcan changesort()in placesorted()new listreverse()in placetupleimmutablereversed()iterator
Why can a list provide sort() and reverse() methods while a tuple cannot?

A tuple cannot be sorted in place because sorting would change the order of its elements. It cannot be reversed in place for the same reason. The absence of sort() and reverse() is therefore not a missing convenience; it follows from the tuple's immutability. The built-in functions solve the problem by creating or producing a separate result instead of changing the tuple.

python
Output
(3, 1, 2)
[1, 2, 3]
(2, 1, 3)

This example demonstrates two different result choices. sorted(values) returns a new list, even though values is a tuple. tuple(reversed(values)) converts the reverse-order iterator into a new tuple. In both cases, values remains unchanged.

Choosing the Right Operation

alternativealternativesame functionsame functionlist.sort()list changessorted(list)new list, ascendingreversed(list)iterator, reverse orderlist.reverse()list changessorted(tuple)new list, ascendingreversed(tuple)iterator, reverse order
What type and ordering result from applying sort(), reverse(), sorted(), or reversed() to lists and tuples?

Preserving Two Orders

A list stores values in the order [4, 2, 5, 1]. You need both the original order and an ascending version.

Identify the requirement: Because the original order is still needed, an in-place method is not the right choice.

Use sorted(): sorted() returns a new list in ascending order and leaves the original list unchanged.

Keep both sequences: The original list remains [4, 2, 5, 1], while the returned list is [1, 2, 4, 5].

Use sorted() when you need a sorted result but must preserve the original order.

Use sort() on a list only when you are certain that the original order will never be needed. Use sorted() when you need to preserve the original list or when the input is an immutable sequence such as a tuple. Use reversed() when you need reverse-order access without modifying the original sequence.

OperationWorks withEffect on originalResult
sort()ListsModifies the list in placeThe list has ascending order
reverse()ListsModifies the list in placeThe list has reverse order
sorted()Lists, tuples, strings, and custom sequence typesDoes not modify the originalA new list in ascending order
reversed()Lists, tuples, strings, and custom sequence typesDoes not modify the originalAn iterator yielding elements in reverse order

Common Mistakes

  • Assuming that tuples have every method that lists have

    Those methods modify a sequence in place, but tuples are immutable.

    Fix: Use sorted(tuple_value) for ascending order or reversed(tuple_value) for reverse-order iteration.

  • Using sort() when the original order is still needed

    sort() modifies the list itself.

    Fix: Use sorted(values) to create a separate ascending list.

  • Expecting sorted() to preserve the input type

    sorted() returns a new list, including when its input is a tuple.

    Fix: Convert the returned list to a tuple if a tuple result is required.

  • Treating reversed() as if it directly returned a list or tuple

    reversed() returns an iterator that yields elements in reverse order.

    Fix: Use list(reversed(values)) or tuple(reversed(values)) when a concrete sequence is needed.

Practice Check

MEDIUM

For each situation, choose the most suitable operation: sort(), reverse(), sorted(), or reversed(). Then explain whether the original sequence changes and what kind of result is produced. 1. A list's old order is no longer needed, and you want the list in ascending order. 2. A list must be displayed from last item to first, but its original order must remain available. 3. A tuple must be ordered from smallest to largest. 4. A tuple must be traversed in reverse order, and you need the result as a tuple.

Hints
  • Use an in-place method only when changing the original list is acceptable.
  • Remember that sorted() returns a list.
  • Remember that reversed() returns an iterator before conversion.

Practice Answers

Match each situation with an operation.

Situation 1: Use sort(). The list is modified in place, and its old order is not required.

Situation 2: Use reversed(). It provides reverse-order iteration without modifying the original list.

Situation 3: Use sorted(). It works with the tuple and returns a new list in ascending order.

Situation 4: Use tuple(reversed(tuple_value)). reversed() supplies the iterator, and tuple() creates the required concrete tuple.

Methods are suitable for intentional in-place changes to lists; built-in functions are suitable when the original must remain unchanged or when the input is immutable.

Key Takeaways

  1. Tuples are immutable, so they do not provide sort() or reverse() methods that would change them in place.
  2. List methods such as sort() and reverse() modify the list itself.
  3. sorted() works on sequences without modifying the original and returns a new list in ascending order.
  4. reversed() works on sequences without modifying the original and returns an iterator that yields elements in reverse order.
  5. Choose methods when an in-place list change is intentional; choose built-in functions when you need to preserve the original or work with an immutable sequence.

Key Takeaways

  • A method such as sort() or reverse() acts on a list and changes it in place.
  • Tuples cannot offer those in-place methods because tuples are immutable.
  • sorted() returns a new list in ascending order and leaves its input unchanged.
  • reversed() returns an iterator in reverse order; convert it to a list or tuple when needed.
  • The correct choice depends on whether the original sequence must be preserved.