Concepts / List Methods: append, remove, and extend

List Methods: append, remove, and extend

A list is mutable, which means its contents can be changed after the list has been created.

  • Programming

Changing Existing Contents

A list is mutable, which means its contents can be changed after the list has been created. One way to change an existing element is to place bracket notation on the left side of an assignment. This reassigns the element at the selected index within the existing list rather than creating a new list.

Key point

The important distinction is between selecting an element and changing the selected element. The index selects a position; the assignment gives that position a new value.

Checking Membership

The in operator checks whether a value exists in a list. Its result is True when the value exists and False when it does not. This is a membership check: it asks whether the value is among the list's elements, rather than selecting one element by index.

OperationQuestion it answersResult described by the source
Index selectionWhich element is associated with this position?One index corresponds to one element
Membership check with inDoes this value exist in the list?True or False
ReassignmentWhat happens when a selected position is placed on the left of an assignment?The existing list is modified in place

Indexing, membership checking, and reassignment perform different roles.

Named List Methods

This article is titled around append, remove, and extend, but the supplied source material does not define the behavior, arguments, return values, or index effects of those three methods. The supplied material does establish the broader idea that lists are mutable and that their contents can change after creation. Do not infer method-specific behavior from mutability alone: a complete explanation of each named method requires method-specific source information.

appendnamed methodlist mutabilityestablished by sourceremovenamed methodmethod-specificdetailsnot suppliedextendnamed methodtopic relates totopic relates totopic relates tobehavior not specifiedbehavior not specifiedbehavior not specified
What can be concluded from the supplied source, and what still requires method-specific documentation?

Mistakes to Avoid

  • Common mistake

    Treating a list as unchangeable after creation

    What it looks like

    Assuming that a list cannot have one of its elements reassigned

    Why it fails

    The source defines lists as mutable.

    Do this instead

    Remember that bracket notation on the left side of an assignment can reassign an element.

  • Common mistake

    Confusing an index with the element at that index

    What it looks like

    Treating the position and the stored value as if they were the same thing

    Why it fails

    A list represents a mapping in which each index position corresponds to one element.

    Do this instead

    Use the index to identify the position, then reason about the element mapped to that position.

  • Common mistake

    Using in as though it selected an index

    What it looks like

    Expecting a membership check to identify one position

    Why it fails

    The in operator checks whether a value exists and returns True or False.

    Do this instead

    Use in for membership questions and index notation for position-based selection.

  • Common mistake

    Assuming the supplied source specifies what append, remove, and extend do

    Why it fails

    The source names those methods in the article topic but does not provide their method-specific behavior.

    Do this instead

    Consult method-specific documentation before making claims about their effects, arguments, or index changes.

Check Your Understanding

EASY

Explain the difference between these three questions: Which element is associated with a particular index? Does a particular value exist in the list? What happens when the selected list position is placed on the left side of an assignment?

Hints
  • Relate the first question to the index-to-element mapping.
  • Relate the second question to the in operator.
  • Relate the third question to mutability and in-place modification.

Key Takeaways

  1. 1
    Lists are mutable, so their contents can change after creation.
  2. 2
    An index identifies a position, and each index corresponds to one list element.
  3. 3
    Bracket notation on the left side of an assignment reassigns the selected element in the existing list.
  4. 4
    The in operator checks membership and returns True when a value exists or False when it does not.
  5. 5
    The supplied source does not provide enough method-specific information to explain the exact behavior of append, remove, and extend.

At a glance

  • Lists can be modified after they are created because they are mutable.
  • Indexes map positions to individual list elements.
  • Reassignment through bracket notation changes the existing list in place.
  • The in operator answers whether a value is present in the list.
  • Append, remove, and extend need additional method-specific definitions before their exact behaviors can be taught accurately.

More concepts starting with L