liveBook · Manning

List

As explained in Learn AI-Assisted Python Programming, Second Edition

An empty list is a list containing no elements.

A list collects many values into one place, letting a function accept a flexible number of options through one parameter. For example, a Scrabble helper can compare a list of candidate words instead of requiring separate parameters for three, five, or fifty words. section 3.3.5

Use lists to:

  • Pass several words or other values to a function as one argument, using square brackets around the values. section 3.3.5
  • Process each value in turn, such as comparing every word’s Scrabble score or finding the longest word. section 3.3.5 section 6.2.1
  • Test ordinary, single-item, tied, and empty-string cases rather than relying on one successful example. section 6.2.1

Choosing the best word

Define best_word(word_list) with one list parameter, examine each word, calculate its score with num_points, and return the highest-scoring word. Call it as best_word(['zap', 'pack', 'quack']), which returns 'quack'; a one-word list returns that word. Calling best_word('zap', 'pack', 'quack') is incorrect because it supplies several arguments instead of one list. section 3.3.5

def best_word(word_list):
    """
    word_list is a list of words.
    
    Return the word worth the most points.
    """

Use the function signature and docstring as a compact starting point for a list-processing function. section 3.3.5

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines lists as collections of values and demonstrates function calls and processing, while deferring broader list details to later treatment.
Examples
Demonstrates Scrabble word selection and longest-word tests using ordinary, single-item, tied, and empty-string lists.
What it assumes
Assumes basic functions, parameters, arguments, and return values while introducing lists as a collection type.
What you can do after
Apply lists through Python function calls and test list-processing behavior across common and edge-case inputs.
How it connects
Connects lists with functions, parameters, arguments, strings, helper functions, and unit testing.

Also explained in

Compare how 21 books explain list →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.