Concepts / List Methods: append(), extend(), and insert()

List Methods: append(), extend(), and insert()

List concatenation with + joins two or more lists into a single new list without modifying the originals.

  • Programming

Two Ways to Build a Larger List

Python provides two operators for combining or duplicating lists. The + operator concatenates lists by placing their elements in sequence. The * operator repeats the elements of a list a specified number of times. Both operations produce a new list, so the original list or lists are not modified.

Reading Concatenated Indices

Concatenation preserves the order of each input list. Every element from the first list occupies the beginning of the new list. The elements from the second list then follow immediately after them. If the first list has three elements, its elements use indices 0 through 2 in the result, and the second list begins at index 3.

a[0]first elementa[1]second elementa[2]third elementb[0]becomes c[3]b[1]becomes c[4]b[2]becomes c[5]
Which index in the combined list contains each original element?
python
Output (expected)
[1, 2, 3, 4, 5, 6]
1
4
[1, 2, 3]
[4, 5, 6]

In this example, c contains six elements. The elements from a occupy c[0] through c[2]. The elements from b begin at c[3], so b[0] becomes c[3], b[1] becomes c[4], and b[2] becomes c[5]. The printed values of a and b show that concatenation did not modify either original list.

Predicting Repeated Blocks

What do you think happens?

A list contains the elements [1, 2, 3]. What structure results from repeating it three times with the * operator?

  • [1, 2, 3, 3, 2, 1]
  • [1, 2, 3, 1, 2, 3, 1, 2, 3]
  • [3, 3, 3, 1, 2, 3]
  • [1, 1, 1, 2, 2, 2, 3, 3, 3]
Reveal answer

Answer: [1, 2, 3, 1, 2, 3, 1, 2, 3]

Repetition copies the complete sequence in its existing order, placing one full block after another.

Repetition duplicates a complete list pattern. If the original list has n elements and it is repeated three times, the result contains three consecutive copies of the original pattern. The first copy occupies the earliest positions, the second copy follows it, and the third copy follows the second.

1first block2first block3first block1second block2second block3second block1third block2third block3third block
How are the three repeated blocks arranged, and which indices belong to each block?
python
Output (expected)
[1, 2, 3, 1, 2, 3, 1, 2, 3]
1
1
1
[1, 2, 3]

The repeated list has nine elements. The first copy uses indices 0 through 2, the second uses indices 3 through 5, and the third uses indices 6 through 8. Notice that repeated values can appear at different indices because each copy occupies a different position in the result.

Separate Result Objects

contributes elementscontributes elementssupplies repeated patterna[1, 2, 3]ca + bb[4, 5, 6]repeatedpattern * 3pattern[1, 2, 3]
Which list objects exist before and after concatenation or repetition, and which originals remain unchanged?

The result of a + or * expression is a new list object. The input lists remain available and unchanged. This matters when the same input list is needed later: assigning the result to another name gives you a larger or repeated list while preserving the original list's contents.

Combining and repeating without changing inputs

Suppose a = [1, 2], b = [3, 4], and pattern = [0]. Determine the resulting lists and the original values after computing a + b and pattern * 4.

Concatenate a and b: Place the elements of a first, followed immediately by the elements of b. The new list is [1, 2, 3, 4].

Repeat pattern: Place the complete one-element pattern four times in sequence. The new list is [0, 0, 0, 0].

Check the inputs: The original lists remain a = [1, 2], b = [3, 4], and pattern = [0]. Neither operation modifies its input.

The operations create new lists: [1, 2, 3, 4] and [0, 0, 0, 0], while all three original lists retain their contents.

Choosing the List Operation

OperationPurposeEffect on the original list
+Join lists in sequenceCreates a new list; originals are not modified
*Repeat a list patternCreates a new list; the original is not modified
append()Add one item at the endThe supplied comparison describes a list-changing method
extend()Add multiple itemsThe supplied comparison describes a list-changing method
insert()Place one item at a particular positionThe supplied comparison describes a list-changing method

Mistakes to Avoid

  • Expecting a + b to change a or b directly.

    Concatenation creates a new list. The expression has a combined result, but the original lists remain unchanged.

    Fix: Store the result when you need to use it later: combined = a + b.

  • Thinking repetition rearranges the elements.

    Repetition places complete copies of the original sequence one after another.

    Fix: Predict consecutive blocks: [1, 2, 3, 1, 2, 3, 1, 2, 3].

  • Using the wrong index after concatenation.

    The second list does not restart at index 0 in combined. Its elements begin after all elements from a.

    Fix: Because a has three elements, b[0] becomes combined[3], b[1] becomes combined[4], and b[2] becomes combined[5].

  • Treating the + and * operations as interchangeable.

    The first operation combines two list operands, while the second repeats one list using an integer.

    Fix: Use + for concatenation and * for repetition.

Practice the Prediction

EASY

Let first = ["red", "blue"] and second = ["green"]. Predict the value of joined, the value at joined[2], and the contents of first after joined = first + second. Then predict the value of repeated = first * 3 and identify which indices contain the first, second, and third copies of the original two-element pattern.

Hints
  • Concatenation places the complete first list before the complete second list.
  • The second list begins at the index equal to the number of elements in the first list.
  • For a two-element pattern repeated three times, the blocks occupy indices 0-1, 2-3, and 4-5.
  1. The + operator concatenates lists in order and returns a new list. The * operator repeats a list pattern and returns a new list. In a concatenated result, the second list begins after all elements of the first. In a repeated result, each complete copy occupies its own consecutive block of indices. Neither operation modifies its original list. The supplied comparison also distinguishes append(), extend(), and insert() as operations for adding one item at the end, adding multiple items, or placing one item at a particular position.

Key Takeaways

  • Use + to concatenate lists into a new list.
  • Use * with a list and an integer to repeat the list pattern.
  • Both operations preserve the original input lists.
  • Concatenated indices continue from the end of the first list.
  • Repeated lists consist of consecutive copies of the original pattern.