0
0
Pythonprogramming~20 mins

List concatenation and repetition in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of list concatenation and repetition
What is the output of the following Python code?
Python
a = [1, 2]
b = [3, 4]
c = a + b * 2
print(c)
A[1, 2, 3, 4, 1, 2, 3, 4]
B[1, 2, 3, 4, 3, 4, 1, 2]
C[1, 2, 3, 4, 3, 4]
D[1, 2, 3, 4, 4, 3]
Attempts:
2 left
๐Ÿ’ก Hint
Remember that list repetition happens before concatenation.
โ“ Predict Output
intermediate
2:00remaining
Result of repeated concatenation
What will be printed by this code?
Python
lst = [0]
lst = lst * 3 + [1]
print(lst)
A[0, 0, 0, 1]
B[0, 1, 0, 1]
C[0, 0, 1]
D[1, 0, 0, 0]
Attempts:
2 left
๐Ÿ’ก Hint
Check the order of operations: repetition then concatenation.
โ“ Predict Output
advanced
2:00remaining
Nested list repetition and concatenation output
What is the output of this code snippet?
Python
x = [[1]]
y = x * 3 + [[2]]
print(y)
A[[1], [1], [1], [2]]
B[[1, 1, 1], [2]]
C[[1], [2], [1], [1]]
D[[1], [1], [2], [1]]
Attempts:
2 left
๐Ÿ’ก Hint
List repetition duplicates references to the inner list.
โ“ Predict Output
advanced
2:00remaining
Effect of list repetition on mutable elements
What will be the output after running this code?
Python
lst = [[0]] * 3
lst[0].append(1)
print(lst)
A[[0], [0], [0, 1]]
B[[0], [0, 1], [0]]
C[[0, 1], [0], [0]]
D[[0, 1], [0, 1], [0, 1]]
Attempts:
2 left
๐Ÿ’ก Hint
Remember that list repetition copies references, not the objects themselves.
๐Ÿง  Conceptual
expert
2:00remaining
Understanding list concatenation and repetition precedence
Which option correctly describes the result of this expression: [1] + [2] * 3?
AA list with two elements: [1, 6]
BA list starting with 1 followed by three 2's: [1, 2, 2, 2]
CA list with four elements all equal to 3: [3, 3, 3, 3]
DA list with three elements: [1, 2, 3]
Attempts:
2 left
๐Ÿ’ก Hint
Recall that repetition happens before concatenation.