Strings: Immutable Character Sequences
Lists and tuples are both sequences that can hold multiple items, so it is natural to expect them to offer similar operations.
The Ordering Problem
Lists and tuples are both sequences that can hold multiple items, so it is natural to expect them to offer similar operations. However, a list can be rearranged in place with methods such as sort() and reverse(), while a tuple cannot. The reason is not that tuples cannot be read or traversed. The reason is that tuples are immutable: their contents cannot be changed in place.
Python solves this by separating two ideas. A method such as sort() or reverse() changes a list itself. A built-in function such as sorted() or reversed() works with a sequence while preserving the original and producing a separate result. This distinction lets you rearrange tuple contents and also lets you rearrange a list without losing its original order.
What do you think happens?
A tuple contains several values. What should you use when you need those values in ascending order but must not change the tuple?
Reveal answer
The sorted() built-in function
Tuples do not provide sort() or reverse() methods because those methods would modify the sequence in place. sorted() returns a new list in ascending order and leaves the tuple unchanged.
In-Place Versus Separate Results
An in-place method acts on the existing list. When sort() or reverse() is used on a list, the list's order is modified directly. This is appropriate when the old order is no longer needed. The operation does not represent a request for a second, independently ordered sequence; it changes the list you already have.
A built-in function takes a sequence and returns another result. sorted() returns a new list in ascending order. reversed() returns an iterator that yields the elements in reverse order. The original sequence is not modified by either built-in function.
Why Tuples Have No Rearrangement Methods
A tuple cannot be sorted in place or reversed in place because both operations would change the order of its elements. That would violate tuple immutability. Therefore, tuples do not provide sort() or reverse() methods.
| Operation | Works with | Result or effect | Original preserved? |
|---|---|---|---|
| sort() | Lists | Reorders the list in place | No |
| reverse() | Lists | Reverses the list in place | No |
| sorted() | Lists, tuples, strings, and other sequences | Returns a new list in ascending order | Yes |
| reversed() | Lists, tuples, strings, and other sequences | Returns an iterator yielding elements in reverse order | Yes |
The operation determines both which sequences can be used and whether the original is changed.
Using Built-In Functions Across Sequences
Reordering a Tuple Without Changing It
A tuple contains values in an order that is not ascending. You need an ascending result while keeping the tuple itself unchanged.
- 1Choose the operation
Because a tuple is immutable, do not look for an in-place
sort()method. Usesorted(), which works on tuples and returns a new list. - 2Track the original
The tuple keeps its original order. Calling
sorted()does not replace that tuple or rearrange its elements. - 3Track the result
The returned value is a new list whose elements are in ascending order.
The tuple remains unchanged, while sorted() supplies a separate ascending list.
The same preservation principle applies when reversing. reversed() can accept a list, tuple, or string. It does not directly return a list or tuple; it returns an iterator that yields elements in reverse order. When a concrete sequence is needed, convert that iterator to a list or tuple.
Choosing the Right Tool
- 1If the input is a tuple or another immutable sequence, use
sorted()orreversed()rather than looking for an in-place method. - 2If the input is a list and the old order is not needed, an in-place method can be appropriate.
- 3If the input is a list and the old order must remain available, use
sorted()orreversed(). - 4Use
sorted()when the desired result is ascending order and a new list is acceptable. - 5Use
reversed()when the desired result is reverse traversal and remember that its direct result is an iterator.
This design makes the intended behavior explicit. An in-place method signals that the existing list will be changed. A built-in function signals that a result will be produced while the input remains available in its original form. Choosing between them is therefore a decision about both data structure and preservation.
Common Misunderstandings
Common mistake Assuming that every sequence has sort() and reverse() methods.
What it looks likeTreating a tuple as though it were a list and expecting an in-place rearrangement method.
Why it failsTuples are immutable, so changing their element order in place is not allowed.
Do this insteadUse sorted() or reversed(), which work with tuples without modifying the original.
Common mistake Using an in-place list method when the original order is still needed.
What it looks likeReordering a list directly and then trying to use its former order later.
Why it failssort()andreverse()modify the list itself.Do this insteadUse sorted() or reversed() when preservation matters.Common mistake Expecting reversed() to return a list or tuple directly.
What it looks likeTreating the direct result of reversed() as though it were already a concrete sequence.
Why it failsreversed()returns an iterator that yields elements in reverse order.Do this insteadConvert the iterator to a list or tuple when a concrete sequence is required.
Common mistake Assuming sorted() changes the input sequence.
What it looks likeExpecting a list or tuple passed to sorted() to be rearranged.
Why it failssorted()returns a new list and leaves the original sequence unchanged.Do this insteadUse the returned list as the reordered result and keep the original when its order is needed.
Check Your Understanding
A tuple must remain unchanged, but you need both its original order and an ascending version. Which built-in function should you choose, what type of result does it return, and why is an in-place method unsuitable?
Hints
- Think about the difference between changing an existing sequence and returning a new one.
- Remember the specific result type returned by
sorted(). - Connect the absence of tuple methods to immutability.
You need to traverse a string from its last character toward its first while preserving the original string. Should you use an in-place method or a built-in function? What does the built-in function produce directly?
Hints
- Strings are included among the sequences supported by the built-in functions described here.
- The operation is reverse traversal rather than ascending sorting.
- Recall that
reversed()produces an iterator.
Key Takeaways
- 1Tuples are immutable, so they do not provide
sort()orreverse()methods that would rearrange them in place. - 2
sort()andreverse()are list methods that modify the list itself. - 3
sorted()works with lists, tuples, strings, and other sequences and returns a new list in ascending order. - 4
reversed()works with those sequences and returns an iterator that yields elements in reverse order. - 5Use built-in functions when the original sequence must remain unchanged or when the sequence is immutable.
At a glance
- In-place methods change an existing list, while built-in functions return separate results.
- Tuples lack
sort()andreverse()because their contents cannot be modified in place. sorted()returns a new list in ascending order and preserves its input.reversed()returns an iterator for reverse-order traversal and preserves its input.- Choose the operation based on whether the original order must remain available.