Concepts / Nested Lists and Multidimensional Structures

Nested Lists and Multidimensional Structures

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

  • Programming

Building Larger List Structures

Python gives you two operators for building larger lists from smaller ones. The + operator places the elements of lists into one longer list. The * operator places repeated copies of one list pattern into a new list. In both cases, the original lists remain unchanged.

The central question is not only what elements appear in the result, but also where each element appears and whether the original lists were changed.

Reading Nested Positions

A nested list can be viewed as a list whose positions contain smaller list-shaped groups. To reason about such a structure, first identify the outer position and then the position inside the selected inner group. This gives a nested index path: the first index selects a group, and the next index identifies an element within that group.

outer position 0outer position 1inner position 0inner position 1inner position 0inner position 1Nested structureOuter index 0[a, b]ainner index 0cinner index 0Outer index 1[c, d]binner index 1dinner index 1
How can you trace an element through an outer list position and then an inner list position?

Joining Lists with Plus

List concatenation is the operation of joining two or more lists with the + operator to produce one new list. The elements from the first list occupy the first positions, and the elements from the next list follow immediately after them.

Tracking Concatenated Indices

Suppose a = [1, 2, 3] and b = [4, 5, 6]. Determine the resulting list and the new positions of the elements that came from b.

Place the first list: The three elements from a occupy indices 0, 1, and 2 in the new list.

Append the second list: The elements from b follow immediately, so b[0] becomes index 3, b[1] becomes index 4, and b[2] becomes index 5.

Check the originals: The operation produces a new list. The source lists a and b are not modified.

The new list is [1, 2, 3, 4, 5, 6]. The elements from b occupy indices 3, 4, and 5.

position 0position 1position 2position 3position 4New listaindex 0bindex 1cindex 2dindex 3eindex 4
After joining [a, b] and [c, d, e], which index refers to each original element?

To find where a later list begins, count the elements in the lists before it. If the first list has three elements, the second list begins at index 3.

Repeating a List Pattern

List repetition uses the * operator with a list and an integer. The result is a new list containing the original list pattern the specified number of times. The original list is not modified.

Tracking Repeated Indices

Suppose pattern = [1, 2, 3] and the pattern is repeated three times. Determine the resulting list and the positions of each copy.

Write the first copy: The first copy preserves the order 1, 2, 3 and occupies indices 0, 1, and 2.

Write the second copy: The second copy begins after the first three elements and occupies indices 3, 4, and 5.

Write the third copy: The third copy occupies indices 6, 7, and 8.

The resulting list is [1, 2, 3, 1, 2, 3, 1, 2, 3]. Each complete copy keeps the order of the original pattern.

thenthenCopy 1indices 0 to 2Copy 2indices 3 to 5Copy 3indices 6 to 8
When a list is repeated three times, in what order do the elements appear and what indices do the copies receive?

Separate Results from Originals

Both operators produce a new list object. Concatenation does not add the second list into the first list in place, and repetition does not expand the original list in place. Instead, each expression produces a result that can be assigned to another name or used as a value.

OperationRequired operandsResultEffect on original list
+ListsOne list with elements placed in sequenceOriginal lists are not modified
*A list and an integerOne list containing repeated copiesOriginal list is not modified

Concatenation joins list contents; repetition duplicates a list pattern.

Mistakes with List Operators

  • Expecting + or * to modify an existing list automatically.

    Both operations create new list objects instead of performing in-place modifications.

    Fix: Assign the result when you need to keep it, such as combined = first + second.

  • Treating concatenation as if it interleaves elements.

    Concatenation places all elements from the first list before the elements from the second list.

    Fix: Read the result in list order: [a, b, c, d, e].

  • Forgetting that repetition needs an integer count.

    The source rule for repetition requires a list and an integer.

    Fix: Use a list on one side of * and an integer on the other side.

  • Resetting the index count when a second list begins.

    The new list has one continuous sequence of indices.

    Fix: Start the later list after the number of elements already placed.

Predict Before Combining

EASY

Suppose first = [1, 2] and second = [3, 4, 5]. Predict the result of first + second. Then identify the index containing the original second[0] and the index containing the original second[2].

Hints
  • Place every element from first before any element from second.
  • The second list begins after the two elements of first.
  • Keep the order 3, 4, 5 unchanged.
EASY

Suppose pattern = [1, 2, 3]. Predict the result of pattern * 2. Identify the indices occupied by the second copy of the pattern.

Hints
  • Write one complete copy before writing the second copy.
  • The second copy begins after three elements.
  • The second copy preserves the order 1, 2, 3.

What do you think happens?

If a = [1, 2] and b = [3, 4], what does a + b produce, and what remains unchanged?

  • It produces [1, 2, 3, 4], while a and b remain unchanged
  • It produces [3, 4, 1, 2], while a is changed
  • It adds b into a and leaves no separate result
Reveal answer

Answer: It produces [1, 2, 3, 4], while a and b remain unchanged.

Concatenation places the first list before the second and creates a new list object rather than modifying the originals.

Key Takeaways

  1. Use + to concatenate lists in order and create one new list.
  2. Use * with a list and an integer to create repeated copies of a list pattern.
  3. Neither operation modifies the original list or lists.
  4. For concatenation, later elements begin immediately after the elements already placed.
  5. For repetition, each complete copy keeps the original element order and receives the next available indices.
  6. For nested structures, reason through the outer position and then the inner position.

Key Takeaways

  • The + operator joins lists into a new list without changing the originals.
  • The * operator repeats a list pattern using an integer and produces a new list.
  • Concatenated indices continue from the end of the earlier list or lists.
  • Repeated indices advance one complete copy at a time.
  • Nested structures can be traced by following an outer index and then an inner index.