Concepts / Method vs. Function: When to Use Each

Method vs. Function: When to Use Each

A method such as sort() or reverse() is associated with a sequence object and changes that list itself.

  • 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.

items[3, 1, 2]items[3, 1, 2]items[1, 2, 3]sorted items[1, 2, 3]sort() changes the listsorted() creates a result
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.

original sequence[3, 1, 2]sorted()new listsorted result[1, 2, 3]reversed()iteratorreverse-orderelements2, 1, 3inputreturnsinputyields
How does the original sequence relate to the object produced by sorted() or reversed()?
Code walkthrough
numbers = [3, 1, 2]
sorted_numbers = sorted(numbers)
reversed_numbers = list(reversed(numbers))

print(numbers)
print(sorted_numbers)
print(reversed_numbers)
  1. 1
    The first line creates a list whose order is 3, 1, 2.
  2. 2
    The second line stores the new sorted list in sorted_numbers.
  3. 3
    The third line converts the reverse-order iterator into a list.
  4. 4
    Printing numbers shows that the original list was not modified.
  5. 5
    Printing sorted_numbers and reversed_numbers shows the two separate results.
  • numbers starts with the original order.
  • sorted(numbers) produces a new list in ascending order.
  • reversed(numbers) produces an iterator, so list() is used to make a concrete list.
  • The original numbers list remains in its original order.
Output
[3, 1, 2]
[1, 2, 3]
[2, 1, 3]

Why Tuples Need Functions

listcan changesort()in placesorted()new listreverse()in placetupleimmutablereversed()iteratorprovidesprovidesworks withworks with
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

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

Preserving Two Orders

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

  1. 1
    Identify the requirement

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

  2. 2
    Use sorted()

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

  3. 3
    Keep both sequences

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

Result

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

Best practice

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

Choose the operation according to whether the original sequence may change and which result form you need.

Common Mistakes

  • Common mistake

    Assuming that tuples have every method that lists have

    What it looks like
    Trying to use sort() or reverse() directly on a tuple
    Why it fails

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

    Do this instead

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

  • Common mistake

    Using sort() when the original order is still needed

    What it looks like

    Calling values.sort() before another part of the program needs the unsorted values

    Why it fails

    sort() modifies the list itself.

    Do this instead

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

  • Common mistake

    Expecting sorted() to preserve the input type

    What it looks like
    Expecting sorted((3, 1, 2)) to return a tuple
    Why it fails

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

    Do this instead

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

  • Common mistake

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

    What it looks like
    Expecting reversed(values) to be a concrete list
    Why it fails

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

    Do this instead

    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.
Worked example

Practice Answers

Match each situation with an operation.

  1. 1
    Situation 1

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

  2. 2
    Situation 2

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

  3. 3
    Situation 3

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

  4. 4
    Situation 4

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

Result

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. 1
    Tuples are immutable, so they do not provide sort() or reverse() methods that would change them in place.
  2. 2
    List methods such as sort() and reverse() modify the list itself.
  3. 3
    sorted() works on sequences without modifying the original and returns a new list in ascending order.
  4. 4
    reversed() works on sequences without modifying the original and returns an iterator that yields elements in reverse order.
  5. 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.

At a glance

  • 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.

Related concepts

More concepts starting with M