Pylerno
English

List operations

Choose between append, extend, remove, and pop based on intent.

On this page

The concept

append adds one item, while extend adds each item from another iterable. insert places an item at a chosen index. remove searches for the first matching value. pop removes by position and returns the removed item; without an index it uses the last item.

Many mutating list methods return None because their purpose is changing the list, not producing a replacement. Treat that as part of their contract. Before removing a potentially missing value, decide whether absence is an error or a normal condition that should be checked.

Worked example

python
numbers = [10, 20, 20]
numbers.append(30)
numbers.extend([40, 50])
numbers.insert(0, 5)
numbers.remove(20)
last_number = numbers.pop()
print(numbers, "Removed:", last_number)
print("Count of 20:", numbers.count(20))
if 30 in numbers:
    print("Index of 30:", numbers.index(30))
del numbers[0]
print(numbers)
numbers.clear()
print(numbers)

Trace the result

  1. Trace the list after each append, extend, and insert.
  2. Only the first 20 is removed by remove; the second remains.
  3. pop returns 50 in this example, while clear empties the remaining list.
text
[5, 10, 20, 30, 40] Removed: 50
Count of 20: 1
Index of 30: 3
[10, 20, 30, 40]
[]
Line-by-line walkthrough

Line 1: numbers = [10, 20, 20]

Evaluate [10, 20, 20], then bind or update numbers with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.

Line 2: numbers.append(30)

Call numbers.append to add one item to the existing list. This call may have an effect even when its return value is not assigned.

Line 3: numbers.extend([40, 50])

Call numbers.extend to add each supplied item to the existing list. This call may have an effect even when its return value is not assigned.

Line 4: numbers.insert(0, 5)

Call numbers.insert to insert an item at a specified list position. This call may have an effect even when its return value is not assigned.

Line 5: numbers.remove(20)

Call numbers.remove to remove the first matching value. This call may have an effect even when its return value is not assigned.

Line 6: last_number = numbers.pop()

Evaluate numbers.pop(), then bind or update last_number with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 7: print(numbers, "Removed:", last_number)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: print("Count of 20:", numbers.count(20))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Lines 9–10: if 30 in numbers:

Evaluate 30 in numbers. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.

Line 10: print("Index of 30:", numbers.index(30))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 11: del numbers[0]

Delete numbers[0]. For an indexed list element, later elements shift to fill its position.

Line 12: print(numbers)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 13: numbers.clear()

Call numbers.clear to empty the existing collection. This call may have an effect even when its return value is not assigned.

Line 14: print(numbers)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

Assigning items = items.append(value) replaces your list binding with None. Call append as a separate statement.

Check your understanding

What does this program print?

python
a = [4, 8]
x = a.pop()
print(x)
Choose one answer

Search documentation

Share this page