Concepts / Creating and Initializing Lists

Creating and Initializing Lists

The assignment list[index] = value replaces the element selected by index in the existing list.

  • Programming

A List That Can Change

A list is not limited to the contents it had when it was created. Lists are mutable, which means their contents can be changed after creation. The change happens when bracket notation selects an element on the left side of an assignment.

Key point

The assignment list[index] = value replaces the element selected by index in the existing list.

Reading Index Positions

A list represents a mapping between indices and elements. Each index position corresponds to exactly one element. Bracket notation selects the element associated with a particular index, so changing the index changes which element is selected.

python
Output
selected refers to "green".
0red1green2bluemaps tomaps tomaps to
Which element does each index position refer to, and how does bracket notation select that element?

Replacing One Element

Worked example

Changing the middle element

Start with the list ["red", "green", "blue"] and replace the element at index 1 with "yellow".

  1. 1
    Select the position

    The expression colors[1] selects the element mapped to index 1.

  2. 2
    Assign a new value

    Writing colors[1] = "yellow" reassigns the selected element.

  3. 3
    Read the resulting list

    The first and third elements remain as they were, while the element at index 1 has the new value.

Result

["red", "yellow", "blue"]

python
Output
colors is now ["red", "yellow", "blue"].
[red, green, blue]before[red, yellow, blue]afterindex 1 reassigned
What changes in the list when an assignment such as list[1] = value replaces one element?

Mutability in Action

Mutability describes what can happen after creation: the contents of a list can be changed. Reassigning an indexed element is one direct way to perform that change. The list is still the existing list, but its contents now reflect the new value at the selected position.

[10, 20, 30]initial contents[10, 25, 30]updated contentsindex 1 reassigned
How can the existing list keep serving as the list being modified while its contents change after creation?
Best practice

When reading or changing a list, identify the index first. Then ask which element that index maps to before deciding whether the assignment selects the intended position.

Checking Membership

The in operator checks whether a value exists in a list. Its result is True when the value is present and False when the value is not present.

python
Output
The first membership test produces True. The second membership test produces False.
greenvalue being checked[red, green, blue]listTruevalue existsFalsevalue does not existinfoundnot found
How does the in operator check list membership and produce a True or False result?
Key point

Membership testing asks whether a value is present; indexed assignment changes the value at a selected position. These operations answer different questions.

Practice and Pitfalls

What do you think happens?

What is the resulting list after this reassignment?

  • ["small", "medium", "large"]

  • ["small", "wide", "large"]

  • ["wide", "medium", "large"]

Reveal answer
Answer:

["small", "wide", "large"]

Index 1 selects the second element, "medium", so assigning "wide" replaces that element.

  • Common mistake

    Assuming a list cannot change after creation.

    What it looks like
    Treating ["red", "green", "blue"] as permanently fixed.
    Why it fails

    Lists are mutable, so their contents can be changed after creation.

    Do this instead

    Use bracket notation on the left side of an assignment when you need to reassign an element.

  • Common mistake

    Changing the wrong element because the index mapping was not checked.

    What it looks like

    Using colors[1] without recognizing which element index 1 selects.

    Why it fails

    Each index position corresponds to one particular element.

    Do this instead

    Map the index to its element before assigning a new value.

  • Common mistake

    Confusing membership testing with reassignment.

    What it looks like

    Reading "green" in colors as an instruction to change the list.

    Why it fails

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

    Do this instead

    Use indexed assignment to change an element and use in to test membership.

EASY

Given the list ["Monday", "Tuesday", "Friday"], identify the element selected by index 1. Then describe the resulting list after reassigning index 1 to "Wednesday". Finally, state whether "Friday" in the list produces True or False.

Hints
  • Start by matching index 1 with its element.
  • Only the selected element is replaced by the assignment.
  • The in operator produces True when the tested value exists in the list.

Key Takeaways

  1. 1
    Lists are mutable, so their contents can be changed after creation.
  2. 2
    A list maps index positions to elements, with each index corresponding to one element.
  3. 3
    Bracket notation on the left side of an assignment reassigns the selected element in the existing list.
  4. 4
    Reassignment changes the list in place rather than creating a new list.
  5. 5
    The in operator checks membership and returns True when a value exists or False when it does not.

At a glance

  • A list is mutable and can be modified after creation.
  • Indices provide the mapping from positions to list elements.
  • An assignment such as list[index] = value replaces the selected element in the existing list.
  • The in operator checks whether a value is present and returns True or False.

Related concepts

More concepts starting with C