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.
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?
Reveal 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.
[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.
numbers = [3, 1, 2]
sorted_numbers = sorted(numbers)
reversed_numbers = list(reversed(numbers))
print(numbers)
print(sorted_numbers)
print(reversed_numbers)- 1The first line creates a list whose order is 3, 1, 2.
- 2The second line stores the new sorted list in
sorted_numbers. - 3The third line converts the reverse-order iterator into a list.
- 4Printing numbers shows that the original list was not modified.
- 5Printing
sorted_numbersandreversed_numbersshows the two separate results.
- numbers starts with the original order.
sorted(numbers)produces a new list in ascending order.reversed(numbers)produces an iterator, solist()is used to make a concrete list.- The original numbers list remains in its original order.
[3, 1, 2]
[1, 2, 3]
[2, 1, 3]Why Tuples Need Functions
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.
(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
Preserving Two Orders
A list stores values in the order [4, 2, 5, 1]. You need both the original order and an ascending version.
- 1Identify the requirement
Because the original order is still needed, an in-place method is not the right choice.
- 2Use sorted()
sorted()returns a new list in ascending order and leaves the original list unchanged. - 3Keep 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.
| Operation | Works with | Effect on original | Result |
|---|---|---|---|
| sort() | Lists | Modifies the list in place | The list has ascending order |
| reverse() | Lists | Modifies the list in place | The list has reverse order |
| sorted() | Lists, tuples, strings, and custom sequence types | Does not modify the original | A new list in ascending order |
| reversed() | Lists, tuples, strings, and custom sequence types | Does not modify the original | An 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 likeTrying to use sort() or reverse() directly on a tupleWhy it failsThose methods modify a sequence in place, but tuples are immutable.
Do this insteadUse 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 likeCalling values.sort() before another part of the program needs the unsorted values
Why it failssort()modifies the list itself.Do this insteadUse sorted(values) to create a separate ascending list.
Common mistake Expecting sorted() to preserve the input type
What it looks likeExpecting sorted((3, 1, 2)) to return a tupleWhy it failssorted()returns a new list, including when its input is a tuple.Do this insteadConvert 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 likeExpecting reversed(values) to be a concrete listWhy it failsreversed()returns an iterator that yields elements in reverse order.Do this insteadUse list(reversed(values)) or tuple(reversed(values)) when a concrete sequence is needed.
Practice Check
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.
- A list's old order is no longer needed, and you want the list in ascending order.
- A list must be displayed from last item to first, but its original order must remain available.
- A tuple must be ordered from smallest to largest.
- 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.
- 1Situation 1
Use
sort(). The list is modified in place, and its old order is not required. - 2Situation 2
Use
reversed(). It provides reverse-order iteration without modifying the original list. - 3Situation 3
Use
sorted(). It works with the tuple and returns a new list in ascending order. - 4Situation 4
Use
tuple(reversed(tuple_value)).reversed()supplies the iterator, andtuple()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
- 1Tuples are immutable, so they do not provide
sort()orreverse()methods that would change them in place. - 2List methods such as
sort()andreverse()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. - 5Choose 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()orreverse()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.