List concatenation and repetition in Python - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to join or repeat lists changes as the lists get bigger.
How does the work grow when we add or repeat lists of different sizes?
Analyze the time complexity of the following code snippet.
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b * 3
This code joins list a with list b repeated three times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying elements from lists to create a new list.
- How many times: The elements of
bare copied 3 times, then elements ofaand the repeatedbare copied once more to form the final list.
When the lists get bigger, the time to copy all elements grows proportionally to their total size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Copy about 10 + 3*10 = 40 elements |
| 100 | Copy about 100 + 3*100 = 400 elements |
| 1000 | Copy about 1000 + 3*1000 = 4000 elements |
Pattern observation: The work grows linearly with the size of the lists and the repetition count.
Time Complexity: O(n * k)
This means the time grows in proportion to the size of the list b times the repetition count k, plus the size of list a.
[X] Wrong: "Repeating a list with * is instant and does not depend on list size."
[OK] Correct: Actually, repeating a list creates a new list by copying elements multiple times, so it takes longer as the list or repetition grows.
Understanding how list operations scale helps you write efficient code and explain your choices clearly in interviews.
"What if we used list.extend() instead of + for concatenation? How would the time complexity change?"
Practice
[1, 2] + [3, 4] do in Python?Solution
Step 1: Understand the + operator with lists
The + operator joins two lists into one new list containing all elements from both.Step 2: Apply + to [1, 2] and [3, 4]
Joining these lists results in [1, 2, 3, 4].Final Answer:
Creates a new list combining both lists: [1, 2, 3, 4] -> Option AQuick Check:
List concatenation = combined list [OK]
- Thinking + repeats lists instead of joining
- Confusing list concatenation with nesting
- Expecting an error when adding lists
[5, 6] three times?Solution
Step 1: Identify the operator for repetition
The * operator repeats a list multiple times when used with an integer.Step 2: Check syntax correctness
[5, 6] * 3 repeats the list three times: [5, 6, 5, 6, 5, 6].Final Answer:
[5, 6] * 3 -> Option DQuick Check:
List repetition uses * operator [OK]
- Using + instead of * for repetition
- Trying to use ** which is invalid for lists
- Confusing order of operands for repetition
lst1 = [1] lst2 = [2, 3] result = lst1 * 2 + lst2 * 3 print(result)
Solution
Step 1: Calculate lst1 * 2
Repeating [1] twice gives [1, 1].Step 2: Calculate lst2 * 3
Repeating [2, 3] three times gives [2, 3, 2, 3, 2, 3].Step 3: Concatenate the two results
Adding [1, 1] + [2, 3, 2, 3, 2, 3] results in [1, 1, 2, 3, 2, 3, 2, 3].Final Answer:
[1, 1, 2, 3, 2, 3, 2, 3] -> Option AQuick Check:
Repeat then join lists = combined repeated list [OK]
- Adding before repeating
- Miscounting repeated elements
- Confusing order of concatenation
list_a = [1, 2] list_b = [3, 4] result = list_a * list_b print(result)
Solution
Step 1: Understand list repetition rules
List repetition uses * with an integer, not another list.Step 2: Identify the error in list_a * list_b
Multiplying two lists causes a TypeError because the right operand must be int.Final Answer:
TypeError because * expects an integer, not a list -> Option CQuick Check:
List * list is invalid, needs int [OK]
- Trying to multiply two lists directly
- Expecting concatenation with * operator
- Confusing syntax error with type error
fruits = ['apple', 'banana']veggies = ['carrot']How do you create a new list that repeats the fruits twice and veggies three times, then combines them all in order?
Solution
Step 1: Repeat fruits and veggies separately
fruits * 2 repeats fruits twice; veggies * 3 repeats veggies three times.Step 2: Concatenate the repeated lists
Adding these repeated lists combines them in order as desired.Final Answer:
fruits * 2 + veggies * 3 -> Option BQuick Check:
Repeat separately then add lists [OK]
- Repeating combined list instead of separate lists
- Using invalid multiplication of lists
- Confusing order of operations
