liveBook · Manning

Tuple

As explained in The Quick Python Book, Fourth Edition

A tuple is an ordered, immutable collection that can contain multiple values, including values of different types.

A tuple is an ordered Python sequence that cannot be modified after creation. It works much like a list for reading values, but suits a fixed group of values—such as a record or a dictionary key. Tuples can hold values of different types. section 3.2.3 section 5.0

Create and read a tuple

x = ('a', 'b', 'c') x[2] # 'c' x[1:] # ('b', 'c') len(x) # 3 Indexing and slicing read from the tuple. Assignment does not: x[2] = 'd' raises TypeError. section 5.7.1

The small syntax and operation differences matter:

  • Use () for an empty tuple and (1,) for a one-element tuple. The comma distinguishes (1,) from a parenthesized expression such as (1). section 5.7.2
  • Membership tests, len, max, min, +, and * work as they do with lists without changing the original tuple. Tuples have only the count and index methods. section 3.2.3
  • Convert with list((1, 2)) to get [1, 2], or tuple([1, 2]) to get (1, 2). Use a list when you need to change elements. section 5.7.4 section 5.7.1

Pack and unpack values

a, b, c = 1, 2, 3 packs the values and assigns them to corresponding variables. To swap two values, write a, b = b, a. Extended unpacking collects surplus values in a list: first, *middle, last = (1, 2, 3, 4) gives middle == [2, 3]. Packing and unpacking also work with lists. section 5.7.3

A tuple itself cannot be changed, but a list or dictionary inside it can still change. That distinction matters for dictionary keys: a tuple is a valid key only when its contents, including nested contents, are hashable. Thus ('filename', 'extension') can be a key, but ('tom', [1, 2, 3]) cannot. section 5.7.1 section 7.4

sole_cache = {}
def sole(m, n, t):
   if (m, n, t) in sole_cache:
       return sole_cache[(m, n, t)]
   else:
       # . . . do some time-consuming calculations . . .
       sole_cache[(m, n, t)] = result
       return result

A dictionary cache uses a tuple of three function arguments as its key. Repeated calls with the same argument combination can return the stored result instead of recalculating it. section 7.6

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through creation, reading, immutability, packing, conversion, and key eligibility without examining tuple implementation internals.
Examples
Demonstrates indexing errors, one-element syntax, starred unpacking, list conversion, and a tuple-keyed cache.
What it assumes
Assumes familiarity with lists, indexing, and assignment while teaching tuple-specific syntax and restrictions.
What you can do after
Practice/perform it by creating and unpacking Python tuples and checking whether they qualify as dictionary keys.
How it connects
Connects tuples to lists, sequence operations, assignment unpacking, hashability, and dictionaries.

Also explained in

Compare how 15 books explain tuple →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.