liveBook · Manning

List

As explained in Get Programming: Learn to code with Python

A list is an ordered collection of elements arranged in a sequence.

Use a list when you need an ordered collection that can change. A grocery list is a familiar example: add items as you remember them, then remove them as you shop. Unlike a tuple, a list is mutable—you can change the same list object instead of making a new one. section 25.0 section 25.1

Create a list with square brackets: `grocery = ["milk", "eggs", "bread"]`. Use `[]` for an empty list. Positions, called indexes, start at 0: `grocery[0]` is `"milk"`, and `len(grocery)` is 3. Trying `grocery[3]` raises an `IndexError` because the last index is 2. section 25.2

Change a list without assigning a new list to its variable:

  • `grocery.append("cheese")` adds one item at the end; `grocery.insert(1, "apples")` adds one at index 1 and shifts later items. `grocery.extend(other_list)` adds another list’s elements in order. section 25.4
  • `grocery.pop()` removes and returns the last item. `grocery.pop(1)` removes and returns the item at index 1; later items shift left. section 25.5
  • `grocery[0] = "oat milk"` replaces the first item. Subsequent operations use the changed list. section 25.6

Lists can hold different object types, including other lists. You can also sort or reverse a list, or split a string into a list. Be careful when two variables refer to the same mutable list: changing it through either name affects the shared object. Use `list(original)` or `original.copy()` when you need a separate list. section 26.0 section 26.5 section 28.0 section 28.2

Listing 26.5. A queue of people represented by a list
line = []                      #1
line.append('Ana')             #2
line.append('Bob')             #3
line.pop(0)                    #4
line.append('Claire')          #5
line.append('Dave')            #5
line.pop(0)                    #6
line.pop(0)                    #6
line.pop(0)                    #6

Follow the queue one line at a time: `append()` places a person at the end, while `pop(0)` removes the person at the front. The list supplies the storage; the chosen operations give it queue behavior. section 26.4

Quick check: after `items = ["pen", "paper"]`, what are `items` and `removed` after `removed = items.pop(0)`? For a larger project, a list can hold the words split from a document before a dictionary counts how often each word appears. section 25.5 section 29.3 section 29.8

How Get Programming: Learn to code with Python covers it

How deep it goes
Works through creation, indexing, mutation, copying, and applications; does not develop list implementation internals.
Examples
Uses grocery lists, pancake stacks, store queues, and document words, alongside short code checks.
What it assumes
Assumes familiarity with tuples and string indexing but teaches list creation and mutation from their first operations.
What you can do after
Build it from scratch: create and change lists, then use them to model a stack or queue.
How it connects
Connects lists to tuples, mutability, stacks, queues, aliasing, copying, and dictionaries.

Also explained in

Compare how 21 books explain list →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.