liveBook · Manning

Chapter 5: Lists, tuples, and sets

What you will learn in chapter 5 of The Quick Python Book, Fourth Edition

Introduces lists and tuples as Python sequence types and contrasts them with sets, where membership matters more than position.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces lists and tuples as Python sequence types and contrasts them with sets, where membership matters more than position.

Key terms: sequence types, lists, tuples, arrays, basic record type, sets, membership, position

5.1 Lists are like arrays

Introduces Python lists as ordered, resizable collections that can hold different kinds of objects, and explains how len counts their elements.

Key terms: list, ordered collection, square brackets, list element, Python object, len function, nested list

5.2 List indices

This section explains how to access individual list elements and use slices to extract or copy portions of a list.

Key terms: list indices, negative numbers, slicing, sublist, copies the list

5.3 Modifying lists

This section explains how to change a list by assigning to indices or slices and by using methods to add, remove, or reorder elements.

Key terms: list index notation, slice notation, slice assignment, append, extend, insert, del, remove, first instance, in

5.4 Sorting lists

This section explains how list sorting changes a list, how to preserve the original, and what determines the default and optional sort order.

Key terms: sort method, in-place sort, sorted(), slice, [:], comparable types, default key method, list of lists, exception, reverse

5.4.1 Custom sorting

This section explains how to sort lists by a custom key, when a lambda can supply that key, and why descending order does not need a custom sort.

Key terms: sort method, key, key function, lexicographical order, len, lambda function, anonymous, sort key, len(x), custom sorting

5.4.2 The sorted () function

This section introduces sorted() for sorting any iterable and asks readers to apply a key function to sort nested lists.

Key terms: sorted(), iterable, key, reverse, sort method, sort() method

5.5 Other common list operations

5.5.1 List membership with the in operator

This subsection shows how to test whether a value is present or absent in a list.

Key terms: in operator, not in operator, Boolean value

5.5.2 List concatenation with the + operator

5.5.3 List initialization with the * operator

This section explains how list multiplication creates a list of a chosen size and why sizing a list in advance can be efficient.

Key terms: list multiplication operator, None, append, memory reallocation overhead

5.5.4 List minimum or maximum with min and max

This subsection explains how min and max find a list’s smallest and largest elements and when comparisons can fail.

Key terms: min, max, list, error

5.5.5 List search with index

This subsection explains how to find a value’s position in a list and what happens if the value is absent.

Key terms: index method, in, list element

5.5.6 List matches with count

This subsection explains that list count returns how many times a value occurs, rather than where it occurs.

Key terms: count, list, value, positional information

5.5.7 Summary of list operations

This subsection points readers to a reference table of list operations and offers practice questions, but does not explain a new concept.

5.6 Nested lists and deep copies

The section explains nested lists, shared references to mutable objects, and why a deep copy is needed to change nested elements independently.

Key terms: nested lists, two-dimensional matrices, two-dimensional indices, variables, objects, mutable, nested list, full slice, shallow copy, deep copy

5.7 Tuples

Introduces tuples as list-like structures that cannot be modified and explains why Python includes them.

Key terms: tuples, lists, keys for dictionaries

5.7.1 Tuple basics

This section introduces tuple creation and use, then explains how immutability affects contained objects and copying.

Key terms: tuple, list, +, *, immutable, mutable objects, dictionary keys, shallow copy, alias

5.7.2 One-element tuples need a comma

This section explains why a one-element tuple needs a trailing comma to distinguish it from a parenthesized expression.

Key terms: one-element tuple, comma, parentheses, empty tuple

5.7.3 Packing and unpacking tuples

This section explains how Python assigns values by packing and unpacking tuples, including starred unpacking for surplus values.

Key terms: packing, unpacking, tuple, extended unpacking, starred element, list

5.7.4 Converting between lists and tuples

This subsection shows how to make a new list or tuple from a sequence, including how to turn a string into a list of characters.

Key terms: list function, tuple function, sequence, string

5.8 Sets

Defines Python sets by their unordered, membership-and-uniqueness-focused nature and identifies which objects can be set members.

Key terms: set, unordered, membership, uniqueness, immutable, hashable, set members, dictionary keys

5.8.1 Set operations

This section shows how to create and change sets, test membership, and combine sets with union, intersection, and symmetric difference.

Key terms: set, sequence, add, remove, in, union, intersection, symmetric difference, |, &

5.8.2 Frozen sets

This subsection explains why frozen sets, unlike ordinary sets, can be members of other sets.

Key terms: set, frozenset, immutable, hashable

5.9 Lab: Examining a list

This lab asks the reader to summarize a list of monthly temperature readings using basic list operations.

Key terms: list, mean, median, min(), max(), sum()

5.9.1 Why solve it the old-fashioned way?

This section asks readers to try the temperature-list exercise themselves before using AI, explaining how that practice improves their ability to prompt for and evaluate generated code.

Key terms: AI code generator, effective prompt, generated code, list of floats, unique values

5.9.2 Solving the problem with AI code generation

This subsection shows how to request AI-generated code for the list-examining lab by writing a focused prompt in Colaboratory.

Key terms: prompt, list, floats, Colaboratory, Generate

5.9.3 Solutions and discussion

5.10 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: The absolute basics Chapter 6: Strings →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.