liveBook · Manning

List

As explained in Python How-To

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

Need to keep a collection of tasks that can change throughout the day? A list holds objects in an ordered sequence. Items are accessible by index, and you can append, insert, replace, or remove them. Its order initially follows insertion order. section 3.1 section 3.1.1 section 3.2

Keep tasks in a changeable sequence

tasks = ["Laundry", "Homework"] tasks.append("Museum") tasks[0] = "Grocery" print(tasks[1:3]) # ['Homework', 'Museum'] The slice starts at index 1 and excludes index 3. Unlike a tuple, the list lets you update its contents after creation. section 3.1.1 section 4.1

Choose the container according to what the data means and how you will use it:

  • Use a list for a changeable collection of items with the same role, such as tasks. A list can contain different types, but a tuple better signals fixed, distinct fields such as a task’s creation time and user. section 3.1.1 section 3.1.2
  • Use an explicit model rather than positional list entries when fields such as title, description, and urgency need clear names. A list of separate task objects remains useful. section 3.3.1 section 4.3.4
  • Use a set or dictionary when repeated membership lookups matter: checking a list for an item requires traversal, while set lookup stays roughly constant as the collection grows. section 3.5.1
  • Use a deque for frequent first-in-first-out removal. Removing a list’s first item with pop(0) shifts the remaining items. section 4.5.2

Check a relationship between lists

good_stocks = ["AAPL", "GOOG", "AMZN", "NVDA"] client_stocks = ["GOOG", "AMZN"] print(set(good_stocks).issuperset(client_stocks)) # True Convert the shared reference list to a set when checking whether it contains every item in another list. This avoids writing a separate membership loop for each check. section 3.6 section 3.6.1

Lists can also be built from iterables: list(range(3)) produces [0, 1, 2]. For a list of dictionaries, plain sort() fails because dictionaries cannot be compared directly; supply a key for the field that should determine order. Prefer the simplest container that fits the operation rather than relying on a list for every collection. section 5.1.3 section 3.2 section 3.7 section 4.5.4

How Python How-To covers it

How deep it goes
Explains container selection, slicing, lookup costs, and FIFO trade-offs through practical cases rather than exhaustively cataloging list methods.
Examples
Uses task collections, stock-list containment checks, custom-class searches, and a customer queue.
What it assumes
Assumes familiarity with basic list methods and indexing while demonstrating container choices and more advanced sequence operations.
What you can do after
Build it from scratch as a mutable collection, then evaluate when a tuple, set, or deque better fits the task.
How it connects
Connects lists to tuples, sets, dictionaries, slicing, iterables, custom classes, and deques.

Also explained in

Compare how 21 books explain list →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.