Concepts / List Methods: append, extend, insert, and remove

List Methods: append, extend, insert, and remove

Assignment creates a reference to a list, not a copy. Both variables point to the same object, so modifications through one affect the other.

  • Programming

Why List Changes Spread

List operations are easier to understand when you first know whether your variables refer to one list or to separate lists. Assignment creates a reference to an existing list; it does not create a copy. Therefore, two variables can be different names for the same list object. If one name is used to modify that list, the other name sees the modification too.

Before changing a list, ask: do I intend to modify the shared original, or do I need an independent list?

Assignment and Copying

refers torefers torefers tooriginalvariable[1, 2, 3]one list objectaliasvariable[1, 2, 3]new list objectcopyseparate variable
Do two variables contain independent lists, or do they point to the same list object?

Consider original = [1, 2, 3] followed by alias = original. The second statement gives alias a reference to the existing list. It does not make a second list containing the same elements. Both names therefore lead to the same object. In contrast, a slice copy using original[:] or a copy made with list(original) creates a new list with the same elements. The two lists then start with equal contents but can be modified independently.

OperationWhat it createsEffect of a later modification
alias = originalAnother reference to the same listA modification through either name is visible through the other
copy = original[:]A new list with the same elementsA modification to copy does not affect original
copy = list(original)A new list with the same elementsA modification to copy does not affect original

Assignment and the two copying techniques described in the source material

Tracing a Shared Mutation

One list, two names

Start with original = [1, 2, 3]. Assign alias = original, then modify the list through alias. What should original show?

Create the list: original refers to a list containing 1, 2, and 3.

Assign the second name: alias receives a reference to the same list. No independent copy is created.

Modify through alias: Because alias and original refer to the same object, the modification is visible through both names.

original and alias both show the modified contents of the same list.

same shared object is modifiedsame shared object is modifiedoriginal[1, 2, 3]originalmodified listalias[1, 2, 3]aliassame modified list
What changes in each variable after one of two shared references modifies the list?

Making an Independent List

Use original[:] or list(original) when you need a separate list with the same elements. This is a shallow copy: the new list itself is separate from the original. After copying, modifying one list does not affect the other list described in the source material.

Preserving the original

An original list contains [10, 20, 30, 40, 50]. Create a separate copy, then apply a replacement to the copy that grows it.

Create the copy: The copy is a new list with the same elements as original, so later list modifications can be kept separate.

Replace part of the copy: The slice [30, 40] is replaced by [200, 201, 202]. The replacement has three elements while the selected slice has two.

Compare the lists: The copy grows from five elements to six elements. The original remains preserved because it is a separate list.

The copied list has six elements, while the original list remains unchanged with five elements.

Choose deliberately. Make a copy when the original must be preserved for later use. Modify the original in place only when changing it is part of the intended program logic, and document that intention when necessary.

Slice Assignment

Slice assignment uses the form t[start:end] = sequence. It replaces multiple elements in a list with the elements from the replacement sequence. Unlike ordinary replacement of one position, the operation can change the list's length when the replacement sequence has a different number of elements.

replace two with tworeplace with fewerreplace with moret[a, b, c, d, e, f]tslice length 2tslice length 2equal replacement[a, x, y, d, e, f] length 6shorter replacementlist shrinkslonger replacementlist grows
How does replacing a slice with fewer, equal, or more elements change the list and its length?
Replacement size compared with selected sliceEffect on list length
EqualThe list keeps the same length
ShorterThe list shrinks
LongerThe list grows

How slice assignment changes list length

Replacing the middle of a list

A list t has six elements, and t[1:3] refers to ['b', 'c']. Replace that slice with ['x', 'y'].

Identify the selected slice: The selected portion contains two elements: ['b', 'c'].

Count the replacement: The replacement sequence ['x', 'y'] also contains two elements.

Determine the length: Because two elements replace two elements, the list contents change but its length does not.

The list remains six elements long, with ['x', 'y'] in the positions previously occupied by ['b', 'c'].

Methods in a Shared-List Context

The important connection among list-changing operations is not only what part of a list they target. It is also whether the list being changed is shared. The source material emphasizes assignment, copying, and slice assignment: these determine whether a later modification affects one independent list or every variable that refers to the same list. Apply that reference check before diagnosing an unexpected result from a list method.

Question to askIf the answer is yesIf the answer is no
Was the second variable created by assignment from the first?Treat both names as references to one shared listContinue checking how the second list was created
Was original[:] or list(original) used?Treat the result as a separate list with the same elementsDo not assume an independent copy
Does the replacement sequence differ in size from the selected slice?Expect the list length to changeExpect the list length to remain the same

Common Reference Mistakes

  • Assuming assignment copies the list

    Assignment creates another reference to the same list object.

    Fix: Use original[:] or list(original) when an independent list is required.

  • Expecting one name to remain unchanged after a shared list is modified

    Both names refer to the same object, so both see the modification.

    Fix: Trace the references before the modification, or create a separate copy first.

  • Assuming slice assignment always preserves list length

    A longer replacement sequence grows the list.

    Fix: Count the selected elements and the replacement elements before predicting the result.

  • Copying only when it is already too late

    The original has already been changed through the shared reference.

    Fix: Make the copy before the operation when the original must be preserved.

Practice Before You Modify

MEDIUM

Suppose original contains [10, 20, 30, 40, 50]. A second variable is created from original, and then a slice in the second variable is replaced with three elements instead of the two elements originally selected. Predict which variable changes and how the list length changes. Then decide what should have been done if original needed to remain unchanged.

Hints
  • First decide whether the second variable is an alias or an independent copy.
  • Compare the number of selected elements with the number of replacement elements.
  • If the original must be preserved, use original[:] or list(original) before modifying.

What do you think happens?

If two variables refer to the same list and a longer sequence replaces a two-element slice through one variable, what should you predict?

  • Only the variable used for the replacement changes
  • Both variables show the changed list, and the shared list grows
  • Neither variable changes because slice assignment needs a copy
Reveal answer

Answer: Both variables show the changed list, and the shared list grows

The variables refer to the same list object, so both see the modification. The replacement is longer than the selected slice, so the list length increases.

Key Takeaways

  1. Assignment creates a reference to a list, not a copy.
  2. Two variables assigned from the same list can refer to one shared object, so a modification through either name is visible through both.
  3. Use original[:] or list(original) to create a separate list with the same elements.
  4. Slice assignment replaces multiple elements and changes list length when the replacement sequence is shorter or longer than the selected slice.
  5. When debugging a list mutation, trace references first and count the selected and replacement elements.

Key Takeaways

  • Assignment and copying are different: assignment shares a list, while original[:] and list(original) create separate lists.
  • A modification through one shared reference is visible through every variable that refers to that list.
  • Slice assignment can replace multiple elements at once.
  • Replacing a slice with fewer, equal, or more elements shrinks, preserves, or grows the list respectively.
  • Trace object sharing and list lengths before debugging an unexpected mutation.