List Comprehensions
List concatenation with + joins two or more lists into a single new list without modifying the originals.
Two Ways to Build Larger Lists
Python provides two fundamental operators for combining and duplicating lists. The + operator concatenates lists by placing their elements in sequence. The * operator repeats a list a specified number of times. Both operations create a new list object, so neither operation modifies the original list or lists.
Joining Lists with Plus
List concatenation joins two or more lists into one new list. The elements from the first list occupy the first positions. The elements from the next list follow immediately after them. The original order within each input list is preserved.
[1, 2, 3, 4, 5, 6]
[1, 2, 3]
[4, 5, 6]The result contains the first list followed by the second list. The two input lists still contain their original elements.Predicting Concatenated Indices
What do you think happens?
Suppose a = [10, 20] and b = [30, 40, 50]. After c = a + b, which value is at c[3]?
Reveal answer
Answer: 40
The two elements from a occupy c[0] and c[1]. The elements from b then occupy c[2], c[3], and c[4], so b[1], which is 40, becomes c[3].
If the first list has length three, its elements occupy indices 0 through 2 in the result. The second list starts at index 3. More generally, the second list's first element appears immediately after the final element of the first list, and the rest of the second list continues from there.
Repeating a List with Asterisk
List repetition uses the * operator with a list and an integer. The integer specifies how many times the list's sequence appears in the new list. For example, repeating [1, 2, 3] three times produces [1, 2, 3, 1, 2, 3, 1, 2, 3]. The original list is not modified.
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 2, 3]A useful way to predict the indices is to treat each repetition as a complete segment. Here the original segment has three elements, so the first copy uses indices 0 through 2, the second uses 3 through 5, and the third uses 6 through 8.
New Results, Unchanged Inputs
The diagram separates the inputs from the result. After first + second, first and second retain their original contents, while combined refers to the newly created list. The same principle applies when a list is repeated: the repeated result is new, and the original list remains unchanged.
Choosing the Right Operator
| Need | Operator | Operand types | Result |
|---|---|---|---|
| Join list contents in sequence | + | List and list | A new combined list |
| Repeat one list pattern | * | List and integer | A new repeated list |
Use concatenation when separate list segments or results need to be merged into one sequence. Use repetition when one pattern needs to appear several times, such as when creating a list with a repeated structure or pattern.
Common Prediction Errors
Expecting + to modify one of the original lists
Concatenation creates a new list object instead of performing an in-place modification.
Fix:
Store or use the result of first + second when you need the combined list.Starting the second list at index 0 in the concatenated result
The second list follows the first list, so its elements receive later indices.
Fix:
Count the elements in the first list first; the second list begins immediately after them.Reading * as a request to multiply individual list values
List repetition duplicates the list's sequence rather than multiplying each element.
Fix:
Copy the complete sequence the specified number of times.Using an operand of the wrong type
Concatenation requires lists on both sides, while repetition requires a list and an integer.
Fix:
Check the operand types before choosing + or *.
Apply the Index Rules
Given left = [7, 8, 9] and right = [10, 11], determine the value at each index of left + right. Then determine the value at each index of [0, 1] * 3. Finally, state which original lists remain unchanged.
Hints
- Place every element of left first, starting at index 0.
- Begin right immediately after the final element of left.
- For repetition, write the complete [0, 1] pattern three times.
Tracing two results
Predict joined = [7, 8, 9] + [10, 11] and repeated = [0, 1] * 3.
Join the lists: The first list supplies indices 0 through 2. The second list follows at indices 3 and 4, so joined is [7, 8, 9, 10, 11].
Repeat the pattern: The two-element pattern appears three times, so repeated is [0, 1, 0, 1, 0, 1].
Check the inputs: The original lists are not modified because both operations create new list objects.
joined = [7, 8, 9, 10, 11] and repeated = [0, 1, 0, 1, 0, 1].
Key Takeaways
- The + operator concatenates two or more lists in order and returns a new list.
- The * operator repeats a list's sequence a specified number of times and returns a new list.
- Concatenation requires list operands; repetition requires a list and an integer.
- After concatenation, the second list begins at the index immediately following the final index of the first list.
- Neither operation modifies its original list or lists.
Key Takeaways
- Use + to place list elements from multiple lists into one new sequence.
- Use * with a list and an integer to repeat the list's sequence.
- Predict concatenated indices by placing the second list after the first.
- Predict repeated indices by treating each copy as a complete segment.
- Both operators create new list objects and leave their original lists unchanged.