Chapter 5: Lists and Tuples
Learn about compound data types, ordered sequences, immutability of tuples, mutability of lists, reference aliasing vs. cloning, and nested tree representations.
Why Do We Need Lists and Tuples?
In Python, we often work with collections of values rather than single individual variables. Grouping related data values together allows us to write structured, reusable algorithms. Python offers built-in ordered sequences such as Lists (mutable, resizeable arrays) and Tuples (immutable, fixed structures) to manage these collections.
Let's first explore the properties and indexing systems of immutable sequences: Tuples.
5.1 Tuples: Ordered Sequences
A tuple is an ordered sequence of elements, expressed as comma-separated values enclosed in parentheses (ratings). Tuples can contain multiple different data types — strings, integers, and floats — all within the same variable.
Type an index below to dynamically locate the element within the tuple object. You can use positive indices (counting from left starting at 0) or negative indices (counting from right starting at -1).
5.2 Concatenation & Slicing: Tuple Concatenation & Slicing Sandbox
Even though tuples are immutable (meaning you cannot modify their contents once created), you can combine multiple tuples to make new ones using concatenation (+), or extract specific ranges using slicing ([start:stop]).
Fact: Immutability Side-Effect
Tuples are immutable, which means we cannot change their elements in-place. When multiple variables point to the same tuple object (aliasing), trying to modify one of them triggers a TypeError. If you reassign the variable to a new tuple, it points to a brand-new object in memory, leaving the other unchanged.
Ratings1 = (10, 9, 8, 7)
Try running this concatenation and sorting example in the interactive playground:
# Tuple Concatenation and Sorting
A = ("pop", 10.0)
B = (1982, "disco", 24.3)
print("Concatenation:", A + B)
print("Sorted B:", sorted(B))
5.3 Nested Tuples (Tree Structure)
A tuple can contain other tuples as well as other complex types (strings, lists). This is called nesting. You can navigate nested levels by chaining square brackets, e.g. NT[2][0]. In computer science, this nesting is represented as a tree structure.
Click on any node in the tree below to see the exact Python code indexing sequence required to access it.
5.4 Lists: Mutable Sequences: Lists Mutability Lab
A list is represented with square brackets ([ratings]). In many respects lists are identical to tuples: they represent ordered sequences and utilize the same positive/negative indexing styles. However, the most critical difference is that lists are mutable. You can change elements, append new ones, or delete items directly in memory.
Perform modifying operations directly on the list and watch the elements update visually and in memory.
Try running this list manipulation example in the interactive playground:
# List Mutability and Deletion
L = ["hard rock", 10, 1.2]
L[0] = "pop"
del L[1]
print("Modified list:", L)
5.5 Modifying Methods: Extend vs. Append
Two essential methods are used to add elements to a list, but they behave differently:
extend(): Expects a list as an argument and concatenates each element of that list to the original list.append(): Adds the argument itself as a single new element (even if that argument is another list, leading to nesting).
String to List: split()
You can convert a string into a list of words using the split() method. By default, it splits on space characters, but you can pass an optional character (delimiter) like a comma to separate csv values.
5.6 Reference: Aliasing vs. Cloning
Because lists are mutable, we have to be careful with references:
- Aliasing (
B = A): Both variables refer to the exact same list in memory. If you change a value via `A`, the change is instantly reflected in `B` (known as a side effect). - Cloning (
B = A[:]): Variable B points to a brand new cloned copy of list A in a different memory location. Changing A does not impact B.
B = ['pop', 10]
Try running this aliasing vs cloning example in the interactive playground to observe side-effects:
# Aliasing vs Cloning Side-Effects
A = ["pop", 10]
B = A
C = A[:] # Cloning
A[0] = "banana"
print("A (Modified):", A)
print("B (Aliased):", B)
print("C (Cloned):", C)
Practice Quiz
Test your knowledge of Python lists and tuples by answering the multiple-choice questions below.