0
0
Pythonprogramming~10 mins

List concatenation and repetition in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - List concatenation and repetition
Start with list A
Concatenate: A + B
New list with elements of A then B
Repeat list A n times
New list with A repeated n times
End
We start with two lists, join them end-to-end for concatenation, or repeat one list multiple times to make a longer list.
Execution Sample
Python
a = [1, 2]
b = [3, 4]
c = a + b
r = a * 3
print(c)
print(r)
This code joins two lists and repeats one list three times, then prints both results.
Execution Table
StepOperationList aList bResult c (a+b)Result r (a*3)Output
1Initialize a[1, 2]N/AN/AN/AN/A
2Initialize b[1, 2][3, 4]N/AN/AN/A
3Concatenate c = a + b[1, 2][3, 4][1, 2, 3, 4]N/AN/A
4Repeat r = a * 3[1, 2][3, 4][1, 2, 3, 4][1, 2, 1, 2, 1, 2]N/A
5Print c[1, 2][3, 4][1, 2, 3, 4][1, 2, 1, 2, 1, 2][1, 2, 3, 4]
6Print r[1, 2][3, 4][1, 2, 3, 4][1, 2, 1, 2, 1, 2][1, 2, 1, 2, 1, 2]
💡 All operations complete, lists concatenated and repeated as expected.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
aundefined[1, 2][1, 2][1, 2][1, 2][1, 2]
bundefinedundefined[3, 4][3, 4][3, 4][3, 4]
cundefinedundefinedundefined[1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4]
rundefinedundefinedundefinedundefined[1, 2, 1, 2, 1, 2][1, 2, 1, 2, 1, 2]
Key Moments - 3 Insights
Why does 'a + b' create a new list instead of changing 'a'?
Because '+' creates a new list combining both lists without modifying the originals, as shown in step 3 of the execution_table.
What happens if we multiply a list by zero or a negative number?
Multiplying by zero or negative number results in an empty list, since repetition count is zero or less, which is not shown here but follows the same logic as step 4.
Does repeating a list with '*' duplicate the elements or references?
It duplicates references to the same elements, so if elements are mutable, changes affect all repeats. Here, elements are integers (immutable), so no side effects.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of r?
A[1, 2, 3, 4]
B[3, 4, 3, 4, 3, 4]
C[1, 2, 1, 2, 1, 2]
D[1, 2]
💡 Hint
Check the 'Result r (a*3)' column at step 4 in the execution_table.
At which step does the list c get its value?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Result c (a+b)' column in the execution_table to see when c is assigned.
If we change 'a' to [5], what will be the output of r after step 4?
A[5, 5, 5]
B[1, 2, 1, 2, 1, 2]
C[5]
D[5, 5]
💡 Hint
Repeating a list multiplies its elements; see variable_tracker for 'a' and 'r' relation.
Concept Snapshot
List concatenation: use '+' to join lists end-to-end.
List repetition: use '*' to repeat list elements multiple times.
Both create new lists without changing originals.
Example: [1,2] + [3,4] = [1,2,3,4]; [1,2] * 3 = [1,2,1,2,1,2].
Full Transcript
This lesson shows how to join two lists using '+' and how to repeat a list multiple times using '*'. We start with two lists a and b. Using a + b creates a new list with elements of a followed by elements of b. Using a * 3 repeats the list a three times in a new list. The original lists a and b do not change. The output prints the concatenated list and the repeated list. This helps understand how list operations create new lists without modifying the originals.