Concepts / List Slicing and Copying

List Slicing and Copying

Python provides several methods for common list operations. Three of the most frequently used are append, extend, and sort. Each modifies the list in place and returns None, but they perform different operations on the list's contents.

  • Programming

One Statement, Two Results

A list method call can have two effects that must be tracked separately: it can change the list itself, and it can produce a return value. For append, extend, and sort, Python changes the existing list in place and the method call returns None. This combination is the source of a common mistake: the list changes successfully, but assigning the method call back to the list variable replaces the variable's list value with None.

In-Place Change and Return Value

call onmodifies in placereturns[3, 1]original listappend(5)method call[3, 1, 5]same list, changedNonereturned value
What changes in the original list, and what value does the method call produce?

Think of append, extend, and sort as instructions applied to an existing list. The instruction changes that list rather than producing a replacement list for you to store. At the same time, the method call has a result: None. Therefore, after a call such as items.append(value), inspect items to see the changed list, but do not expect the call itself to be a new list.

items = [3, 1] result = items.append(5) print(items) print(result)

Three List Operations

MethodOperation on the listReturn value
appendAdds an element to the listNone
extendAdds elements from another iterable to the listNone
sortChanges the order of the list's contentsNone

append, extend, and sort all modify a list in place, but they do different jobs. append adds an element. extend adds elements from another iterable. sort changes the order of the list's contents. The difference between append and extend is especially important when combining lists or working with nested data because the argument is treated differently by the two methods.

resulting contentsresulting contentsappendadds an element[1, 2, [3, 4]]argument kept as oneelementextendadds elements[1, 2, 3, 4]elements added separately
Does the operation add one item as a single element, or add each element from another iterable?
python
Output
[1, 2, [3, 4]]
[1, 2, 3, 4]

Tracing a Sort Call

What do you think happens?

After this code runs, what are the values of scores and returned?

  • scores is [1, 3, 5] and returned is None
  • scores is [5, 3, 1] and returned is [1, 3, 5]
  • scores is None and returned is [1, 3, 5]
Reveal answer

Answer: scores is [1, 3, 5] and returned is None

sort changes the existing list in place and returns None. The list's contents are reordered, but the method call does not produce a replacement list.

scores = [5, 3, 1] returned = scores.sort() print(scores) print(returned)

call onreorders in placereturns[5, 3, 1]before sortsortin-place method[1, 3, 5]same list after sortNonereturn value
What is the list's order before and after sort, and does sort create a separate list?

The Assignment Trap

  • Assigning append back to the list variable.

    append changes items in place but returns None. The assignment therefore replaces the list stored in items with None.

    Fix: Call items.append(3) without assigning its result.

  • Assigning extend back to the list variable.

    extend changes the existing list and returns None.

    Fix: Use items.extend([3, 4]) by itself, then use items to inspect the changed list.

  • Assigning sort back to the list variable.

    sort reorders the existing list and returns None, so the assignment discards the list value held by scores.

    Fix: Use scores.sort() without assignment.

  • Treating append and extend as identical operations.

    append adds an element, while extend adds elements from another iterable. Their arguments are treated differently.

    Fix: Choose append when adding one element and extend when adding elements from another iterable.

Practice the Trace

MEDIUM

For each statement, predict the final list and the method call's return value before checking your reasoning: values = [4, 2]; returned = values.append(6). Then repeat with values = [4, 2]; returned = values.extend([6, 8]). Finally, use values = [4, 2]; returned = values.sort().

Hints
  • Separate the list's state from the value stored in returned.
  • append adds one element.
  • extend adds elements from another iterable.
  • sort changes the order of the existing list.

Tracing extend

Determine the final values after values = [4, 2]; returned = values.extend([6, 8]).

Initial state: values contains [4, 2].

Apply the method: extend adds the elements from [6, 8] to the existing list, so values becomes [4, 2, 6, 8].

Track the return value: extend modifies the list in place and returns None, so returned becomes None.

values is [4, 2, 6, 8], and returned is None.

Practical Boundary

The source material for this lesson establishes the behavior of append, extend, and sort: each operates in place and returns None. It does not specify slice syntax or separate copying behavior, so those details should not be inferred from these three methods. When analyzing a list statement, identify the exact method being called first, then trace its in-place change and its return value.

  1. The central distinction is between changing a list and returning a value. append adds one element, extend adds elements from another iterable, and sort changes the list's order. All three modify the existing list in place and return None. Use these methods without assigning their results back to the list variable. When debugging, trace the list state and the method return value as two separate results.

Key Takeaways

  • append, extend, and sort modify a list in place.
  • append adds an element, extend adds elements from another iterable, and sort changes the list's order.
  • Each of these method calls returns None.
  • Assigning the result of one of these methods back to the list variable replaces the variable's list value with None.
  • To debug a list operation, trace the changed list and the return value separately.