Concepts / Strings: Immutable Character Sequences

Strings: Immutable Character Sequences

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

  • Programming

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?

  • The tuple's sort() method
  • The sorted() built-in function
  • The tuple's reverse() method
  • A list-only in-place operation
Reveal answer

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.

sort() changessorted() returnsitems[3, 1, 2]items[3, 1, 2]items[1, 2, 3]sorted result[1, 2, 3]
What differs when a list is rearranged in place compared with when a built-in function creates a separate result?

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.

supportsrequireslistsort(), reverse()in-place changeallowed for listtuplesorted(), reversed()new resultpreserves tuple
Why can a list use in-place rearrangement methods while a tuple must use functions that return a separate result?
OperationWorks withResult or effectOriginal preserved?
sort()ListsReorders the list in placeNo
reverse()ListsReverses the list in placeNo
sorted()Lists, tuples, strings, and other sequencesReturns a new list in ascending orderYes
reversed()Lists, tuples, strings, and other sequencesReturns an iterator yielding elements in reverse orderYes

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.

Choose the operation: Because a tuple is immutable, do not look for an in-place sort() method. Use sorted(), which works on tuples and returns a new list.

Track the original: The tuple keeps its original order. Calling sorted() does not replace that tuple or rearrange its elements.

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

provide sequencereturnsoriginal sequencelist, tuple, or stringsorted() orreversed()built-in functionnew resultlist or iterator
Which sequence remains unchanged, and where does the reordered data go when a built-in function is used?

Choosing the Right Tool

  1. If the input is a tuple or another immutable sequence, use sorted() or reversed() rather than looking for an in-place method.
  2. If the input is a list and the old order is not needed, an in-place method can be appropriate.
  3. If the input is a list and the old order must remain available, use sorted() or reversed().
  4. Use sorted() when the desired result is ascending order and a new list is acceptable.
  5. Use 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

  • Assuming that every sequence has sort() and reverse() methods.

    Tuples are immutable, so changing their element order in place is not allowed.

    Fix: Use sorted() or reversed(), which work with tuples without modifying the original.

  • Using an in-place list method when the original order is still needed.

    sort() and reverse() modify the list itself.

    Fix: Use sorted() or reversed() when preservation matters.

  • Expecting reversed() to return a list or tuple directly.

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

    Fix: Convert the iterator to a list or tuple when a concrete sequence is required.

  • Assuming sorted() changes the input sequence.

    sorted() returns a new list and leaves the original sequence unchanged.

    Fix: Use the returned list as the reordered result and keep the original when its order is needed.

Check Your Understanding

EASY

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

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

  1. Tuples are immutable, so they do not provide sort() or reverse() methods that would rearrange them in place.
  2. sort() and reverse() 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.
  5. Use built-in functions when the original sequence must remain unchanged or when the sequence is immutable.

Key Takeaways

  • In-place methods change an existing list, while built-in functions return separate results.
  • Tuples lack sort() and reverse() 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.