Concepts / Understanding Lists: Creation and Modification

Understanding Lists: Creation and Modification

Tuples are immutable sequences indexed by integers, meaning their elements cannot be changed after creation.

  • Programming

A Sequence That Stays Fixed

Tuples look similar to lists because both are sequences of values that can be accessed by integer positions. Their important difference is what happens after creation: a list can have items added, removed, or modified, while a tuple keeps its elements fixed. That fixed structure is useful when changing the sequence would be an error.

Think of a tuple as an ordered sequence whose contents are locked after the sequence is created.

Tuple Creation and Structure

A tuple groups related values into one ordered sequence. In the notation used in this lesson, the values are separated by commas and surrounded by parentheses. For example, (10, 20) represents a two-element tuple: the first element is 10 and the second element is 20. The order matters because each value occupies a particular integer position.

(tuple boundary10first element,element separator20second element)tuple boundary
How does tuple notation create one ordered sequence, and why does the comma separate its elements?

Integer Positions and Access

Tuple elements are accessed with the bracket operator and integer indices. Index 0 identifies the first element, and index -1 identifies the last element. Positive positions move from the beginning of the tuple; negative positions count from the end. An index must identify an existing position. If it does not, the requested element cannot be accessed.

next positionnext positionlast elementdoes not map to010120230-130out-of-range indexno element
How does each integer index map to an element in the tuple, and what happens when an index is out of range?

Reading Positions in a Tuple

Consider the tuple (10, 20, 30). Identify the element at index 0, the element at index -1, and the result of requesting a position beyond the tuple's elements.

First position: Index 0 refers to the first element, so it identifies 10.

Last position: Index -1 refers to the last element, so it identifies 30.

Unavailable position: A position beyond the tuple's available elements does not identify an element and cannot be accessed.

The tuple is ordered, and its positions are accessed through integer indices: 0 maps to 10 and -1 maps to 30.

Why Tuple Contents Cannot Change

Immutability means that a tuple's elements cannot be changed after creation. Attempting to replace an element, add an element, or remove an element conflicts with the tuple's fixed structure. This is different from a list, whose contents can be modified after creation.

operation requestedcontents do not changemodification is not allowedcontainscontains(10, 20)tuple contents10element20elementmodificationattemptreplace, add, or remove(10, 20)contents remain fixedblockedmodificationtuple is immutable
What changes when code attempts to replace, add, or remove an element from an existing tuple?

Tuples as Dictionary Keys

Tuples are hashable because they are immutable. This allows an entire tuple to be used as a dictionary key. Lists do not have this capability. A tuple can therefore represent a fixed group of values used to identify one dictionary entry, such as a coordinate-like pair or another grouping whose contents should not change.

used as keymaps to(10, 20)immutable tuple keydictionarykey-value mappingentry valuevalue for the key
How can one tuple serve as a dictionary key, and how does that key connect to the value stored for it?
PropertyTupleList
Indexed by integersYesYes
Elements can be modified after creationNoYes
Can be used as a dictionary keyYesNo
Suitable when contents should remain fixedYesNo; lists are better when frequent modification is needed

Useful Tuple Operations

Immutability does not prevent every operation on a tuple. Most sequence operators also work with tuples. Tuples can be concatenated, repeated, sliced, and checked for membership. They can also be compared element by element from left to right. Two tuples are equal when they have the same length and corresponding elements are equal. For ordering comparisons, Python examines the first elements; if those are equal, it continues to the next positions.

Choosing a Tuple or List

A group of values represents a fixed coordinate-like structure and should not be changed accidentally. Which sequence type fits that requirement?

Identify the required behavior: The sequence's contents should remain fixed after creation.

Compare the sequence types: Tuples are immutable, while lists are designed for adding, removing, or modifying items.

Select the structure: A tuple communicates and enforces the fixed-content requirement.

Use a tuple when the sequence has a fixed structure or when accidental modification would be a bug.

Mistakes with Tuple Modification

  • Treating a tuple like a list after it has been created.

    A tuple is immutable, so its elements cannot be changed after creation.

    Fix: Choose a list when the sequence must be modified frequently. Choose a tuple when its contents should remain fixed.

  • Forgetting that tuple positions are integer-based.

    Tuple elements are accessed using integer indices and the bracket operator.

    Fix: Use index 0 for the first element and -1 for the last element, while ensuring the requested position exists.

  • Assuming that every sequence can be a dictionary key.

    Immutability makes tuples hashable, while lists do not have this dictionary-key capability.

    Fix: Use a tuple when the complete sequence needs to serve as a dictionary key.

Practice and Summary

EASY

Consider the tuple (5, 15, 25). State which element is selected by index 0, which element is selected by index -1, and whether the tuple can be changed to contain 35 instead of 25. Then decide whether this tuple is a better fit than a list for a fixed group of related values.

Hints
  • Index 0 identifies the first element.
  • Index -1 identifies the last element.
  • Ask whether the sequence's contents should remain fixed.
  1. Tuples are immutable, integer-indexed sequences. Create them as ordered groups of values, access their elements with integer indices, and remember that their elements cannot be replaced, added, or removed after creation. Their immutability makes them hashable, so tuples can be used as dictionary keys. Use tuples for fixed structures and lists when the sequence must remain flexible.

Key Takeaways

  • Tuples are immutable sequences whose elements cannot be changed after creation.
  • Tuple elements are accessed with integer indices; index 0 is the first element and index -1 is the last.
  • A requested position must exist in the tuple for access to succeed.
  • Tuple immutability makes tuples hashable and suitable as dictionary keys, unlike lists.
  • Choose tuples for fixed structures and lists when frequent modification is required.