Sequences

6. Sequences#

This chapter presents Python’s sequence types for storing values in order: lists and tuples. You will compare mutable and immutable sequences, and see how aliasing and copying affect programs that share lists, so you can choose the right structure for a task.

  • Creating and using lists and tuples

  • Accessing sequences with indexes and slices

  • Adding, removing, and transforming lists

  • Comparing mutability, identity, aliasing, and copying

  • Applying common sequence idioms used in everyday Python

Chapter Video

This overview introduces Python’s sequence containers, lists and tuples. Its last part covers sets, which Chapter 7 presents in detail.

Python Tutorial for Beginners 4: Lists, Tuples, and Sets (29:04) by Corey Schafer

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Construct and manipulate Python lists using indexing, slicing, and core list methods

  2. Explain object identity, aliasing, shallow copies, and deep copies

  3. Predict how lists behave when passed to functions

  4. Use list comprehensions, unpacking, enumerate(), zip(), and sorting patterns

  5. Use tuples for fixed records, including tuple unpacking, returning multiple values from a function, packing arguments with *args, and named tuples

Chapter Flow

Glossary

#

Term

Explanation

1

Collection

An object that stores multiple values.

2

Sequence

An ordered collection whose items can be accessed by position.

3

List

An ordered, mutable sequence of values.

4

Tuple

An ordered, immutable sequence of values.

5

Mutability

Whether an object can be changed after it is created.

6

Immutable

Describes an object whose contents cannot be changed after creation.

7

Index

An integer position used to access an item in a sequence.

8

Slice

A selected range of items from a sequence.

9

List comprehension

A compact expression for building a list from an iterable.

10

Nested list

A list that contains other lists as elements.

11

Aliasing

Two or more names referring to the same object.

12

Shallow copy

A copy of a collection that still shares nested objects with the original.

13

Deep copy

A copy that recursively copies nested objects.

14

Unpacking

Assigning items from a collection to separate names.

15

enumerate

A built-in function that produces index-value pairs during iteration.

16

zip

A built-in function that pairs items from multiple iterables.

17

Identity

The property that distinguishes one object from another; is checks whether two names refer to the same object.

18

Reference

The association between a variable and the object it refers to.

19

Named tuple

A tuple whose fields can also be accessed by name, created with namedtuple from the collections module.

20

Key function

A function passed as the key argument to sorted, min, or max that computes the value used to compare each item.

21

Record

A group of related values that describe one thing, such as one sale or one student, often stored as a tuple.

22

Argument packing (*args)

Collecting any number of extra positional arguments into a tuple through a parameter whose name begins with *.

23

Equivalent

Describes two objects that have the same value, which == checks.

24

Identical

Describes two names that refer to the same object, which is checks.

25

Lexicographic comparison

Comparing two sequences element by element from the start, so the first pair of items that differ decides the result.

Chapter Overview Slides