Concepts / Modifying lists with methods

Modifying lists with methods

A list is a sequence of values that can contain any data type, unlike strings which contain only characters.

  • Programming

A List Can Change Shape

When a program needs to collect values, a list provides one sequence that can hold those values together. A list may begin empty and later contain elements as the program runs. Understanding what the list contains, which values count as its elements, and what variable refers to it gives you the foundation for modifying lists with methods.

[]zero elements["task", 3]two elementselements added later
What changes when a valid empty list becomes a list containing values?

List Structure and Syntax

A list is a sequence of values. Python creates a list with square brackets, placing elements inside and separating those elements with commas. The elements are the individual values held by the list. For example, in [4, 8, 15], the list has three elements: 4, 8, and 15.

Definition

An empty list is a valid list containing zero elements. It is written as [] and can be used as a starting point for building a list dynamically.

Lista sequence of values4, "red", 2.5mixed data typesStringa sequence of characters"red"characterscan containcontains
How does a list differ from a string in the kinds of values it can contain?

A string is a sequence containing only characters. A list is more flexible: its values do not have to be characters or even the same type. One list can contain integers, strings, floating-point values, and other types together.

Nested Values

A list can contain another list as one of its elements. The inner list is called a nested list. It remains a complete list, but it is stored as one element of the outer list. This lets a list organize data hierarchically.

["spam", 2.0, 5,[10, 20]]outer list"spam"outer element10inner element2.0outer element20inner element5outer element[10, 20]nested list and outerelementcontainscontainscontainscontainscontainscontains
Which values belong directly to the outer list, and which values belong to the nested list?

In the source example ["spam", 2.0, 5, [10, 20]], the outer list has four elements. Those elements are the string "spam", the float 2.0, the integer 5, and the nested list [10, 20]. The values 10 and 20 belong to the nested list rather than being separate top-level elements of the outer list.

Names and List Data

Lists can be assigned to variables just as numbers and strings can. The variable name then refers to the entire list. Using the name gives the program a way to work with that list rather than repeatedly writing the complete square-bracket expression.

scoresvariable name[8, 12, 20]list datarefers to
How is a variable name connected to the list data it refers to?
Worked example

Starting with an Empty List

Represent a collection that begins with no elements and can receive values as a program runs.

  1. 1
    Create the list

    Use [] to create a valid empty list with zero elements.

  2. 2
    Give it a name

    Assign the list to a variable so the variable name refers to the entire list.

  3. 3
    Use it as a starting point

    The empty list can later contain elements as the program runs.

Result

An empty list is both a complete list object and a useful starting point for building a collection dynamically.

Reading a Modification

Before modifying a list, identify its current elements and their structure. For a list such as ["ready", 2, [5, 6]], the outer list contains three elements: a string, an integer, and a nested list. If values are added later, the list has a new collection of elements, but the nested list remains a list inside the outer list.

Before
[]

The list has zero elements.

After
["ready", 2, [5, 6]]

The outer list has three elements: a string, an integer, and a nested list.

This before-and-after view emphasizes what to inspect when a list changes: count the outer elements, identify each value's type, and notice whether one of those elements is itself another list. The source material establishes that an empty list can later receive elements, but it does not specify named list methods or their individual syntax.

Mistakes to Avoid

  • Common mistake

    Treating a list as if it could contain only one data type

    What it looks like
    [4, "four", 4.0]
    Why it fails

    Lists can contain mixed data types, including integers, strings, floats, and other types.

    Do this instead

    Inspect each element independently instead of assuming that every element has the same type.

  • Common mistake

    Forgetting that a nested list is one element of the outer list

    What it looks like
    ["spam", 2.0, 5, [10, 20]]
    Why it fails

    The outer list has four elements. The values 10 and 20 belong inside the fourth element, which is the nested list.

    Do this instead

    Separate the outer list's elements from the inner list's elements.

  • Common mistake

    Assuming [] means an invalid or missing list

    What it looks like
    []
    Why it fails

    An empty list is a valid list with zero elements.

    Do this instead

    Treat [] as a useful starting point for a list that will receive elements later.

  • Common mistake

    Confusing a variable name with the list it refers to

    What it looks like

    A name such as scores refers to the entire list assigned to it.

    Why it fails

    The variable name provides a way to work with the list; it is not the collection of elements itself.

    Do this instead

    Think of the name as a reference to the complete list data.

  • Common mistake

    Assuming a list and a string have the same contents

    What it looks like

    A string contains characters, while a list can contain values of different types.

    Why it fails

    The two structures differ in the kinds of values they contain.

    Do this instead

    Identify whether the value is a string of characters or a list of values.

Practice Check

EASY

Consider the list ["alpha", 7, ["beta", "gamma"]]. How many elements does the outer list contain? Which element is nested? What data types appear in the outer list?

Hints
  • Count the comma-separated values at the outermost level.
  • The bracketed expression inside the outer list is one element of that outer list.
  • Look at the type of each outer element separately.
Worked example

Practice Answer

Analyze ["alpha", 7, ["beta", "gamma"]].

  1. 1
    Count outer elements

    There are three outer elements: "alpha", 7, and ["beta", "gamma"].

  2. 2
    Find the nested element

    ["beta", "gamma"] is the third outer element and is itself a list.

  3. 3
    Identify outer types

    The outer list contains a string, an integer, and another list.

Result

The outer list has three elements and contains mixed data types, including a nested list.

Key Takeaways

  1. 1
    A list is a sequence of values, while a string is a sequence of characters.
  2. 2
    Square brackets create lists, and [] creates a valid empty list with zero elements.
  3. 3
    Lists can contain mixed data types and can contain other lists as nested elements.
  4. 4
    A variable assigned a list refers to the entire list, giving the program a name through which it can work with that data.
  5. 5
    To understand a list modification, inspect the outer elements, the value types, and any nested list structure.

At a glance

  • Lists are sequences that can contain values of any data type, unlike strings, which contain only characters.
  • Square brackets create lists, including empty lists and lists containing nested lists.
  • A list may contain mixed values such as strings, integers, floating-point values, and other lists.
  • A variable name refers to the entire list assigned to it.
  • An empty list can serve as a starting point for building a collection as a program runs.

Related concepts

More concepts starting with M