QuestenaPractice that shows what to review next
Topic lesson

Lists: separate mutation, return values and sharing

About 6 min

Questions stay in the language in which they were published.

These questions ask you to predict list contents, method results and the effects of sharing. Draw the outer list in order, then track mutation, return value and object identity as three separate facts.

Trace the outer sequence and its element references

Lists preserve element order, can change in place and may mix types. Append adds its argument as one new final element, whereas extend reads an iterable and adds each produced element. Try values = list() followed by values.append('ab'): the list gains one string, not two characters.

OperationEffect and result
insert at position iPlace the new element before the element currently at i; do not replace that element.
remove a valueDelete the first equal element; raise ValueError if no equal element exists.
pop with no argumentRemove and return the final element; raise IndexError for an empty list.
sort methodRearrange the existing list and return None.
sorted functionBuild and return a new sorted list while leaving the input unchanged.

Assignment does not copy a list. Try b = a: both names refer to the same outer object, so a later mutation through either name is shared. The copy method and a full slice create a new outer list, but the copy is shallow; nested mutable elements still refer to the same objects.

Repetition also copies element references rather than recursively copying elements. If one repeated element is a mutable inner list, every repeated position can lead to that same inner object. Membership follows a different rule: value in items compares the sought value for equality with elements, so identical object identity is not required.

Reject the tempting wrong routes

TrapHow to reject it
Swapping append and extendAsk whether the argument itself becomes one element or supplies several elements. That single distinction determines nesting.
Treating insert as replacement or placing the new value after the named positionInsert means make space before the current element. Indexed assignment is the operation that replaces.
Reading remove as an index operation or as delete every matchRemove searches by equality and stops after the first match. Absence raises ValueError rather than doing nothing.
Expecting every sorting operation to return a sorted listSeparate method from function: sort mutates and returns None, while sorted returns a new list and preserves the input.
Assuming assignment, a shallow copy or repetition recursively duplicates nested dataTrace each reference. Assignment shares the outer list; a shallow copy separates only the outer list; repetition can repeat the same inner reference.

Try it

Question 25

Which statements accurately classify Python lists? Select all that apply.

  1. A list preserves element order.
  2. A list can be changed in place.
  3. Every element must have the same type.
  4. One list may contain objects of different types.
Question 30

For `items.pop()` with no argument, which statements are true? Select all that apply.

  1. It returns the element it removes.
  2. It removes the first element by default.
  3. It removes the final element by default.
  4. It raises `IndexError` when the list is empty.
Question 35

Two distinct objects compare equal. If one is an element of `items`, what does `value in items` use to find it?

  1. Only whether `value` is a valid index
  2. Equality against the elements
  3. Only object identity against the elements
  4. The textual type names of the elements
Start drillPractice this topic
Lists: separate mutation, return values and sharing · Questena