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

[3, 1]original listappend(5)method call[3, 1, 5]same list, changedNonereturned valuecall onmodifies in placereturns
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.

Code walkthrough
items = [3, 1]
result = items.append(5)
print(items)
print(result)
  1. 1
    Before the call, items contains [3, 1].
  2. 2
    The append operation changes items to [3, 1, 5].
  3. 3
    The method call returns None, so result refers to None.
  4. 4
    The list and the return value are different things: items is the changed list, while result is None.
  • items starts as a list with two elements.
  • append changes the existing list by adding 5.
  • result receives the method call's return value, which is None.
Output
[3, 1, 5]
None

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

The source identifies append, extend, and sort as in-place list methods that return None.

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.

appendadds an element[1, 2, [3, 4]]argument kept as oneelementextendadds elements[1, 2, 3, 4]elements added separatelyresulting contentsresulting contents
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.

Code walkthrough
scores = [5, 3, 1]
returned = scores.sort()
print(scores)
print(returned)
  1. 1
    Before sort, scores contains [5, 3, 1].
  2. 2
    After scores.sort(), the same list contains [1, 3, 5].
  3. 3
    The call scores.sort() returns None.
  4. 4
    The variable returned therefore contains None rather than a sorted list.
  • scores starts with the contents [5, 3, 1].
  • sort changes the order of the existing list.
  • returned receives None from the sort method call.
Output
[1, 3, 5]
None
[5, 3, 1]before sortsortin-place method[1, 3, 5]same list after sortNonereturn valuecall onreorders in placereturns
What is the list's order before and after sort, and does sort create a separate list?

The Assignment Trap

Before
items = [1, 2]
items = items.append(3)
print(items)

The method changes the list to [1, 2, 3], then the assignment stores None in items.

After
items = [1, 2]
items.append(3)
print(items)

The method changes items in place, and items remains the changed list [1, 2, 3].

  • Common mistake

    Assigning append back to the list variable.

    What it looks like
    items = items.append(3)
    Why it fails

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

    Do this instead

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

  • Common mistake

    Assigning extend back to the list variable.

    What it looks like
    items = items.extend([3, 4])
    Why it fails

    extend changes the existing list and returns None.

    Do this instead

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

  • Common mistake

    Assigning sort back to the list variable.

    What it looks like
    scores = scores.sort()
    Why it fails

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

    Do this instead
    Use scores.sort() without assignment.
  • Common mistake

    Treating append and extend as identical operations.

    What it looks like

    Using append when the elements of another iterable should be added separately.

    Why it fails

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

    Do this instead

    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.
Worked example

Tracing extend

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

  1. 1
    Initial state

    values contains [4, 2].

  2. 2
    Apply the method

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

  3. 3
    Track the return value

    extend modifies the list in place and returns None, so returned becomes None.

Result

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. 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.

Related concepts

More concepts starting with L