Lists & Tuples · Question 16 · Hard

The list a is extended once with + and once with += through a second name. What does this print?

a = [1, 2, 3]
b = a
a = a + [4]
b += [5]
print(a, b)
  1. [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
  2. [1, 2, 3, 4] [1, 2, 3, 5]
  3. [1, 2, 3, 4] [1, 2, 3, 4, 5]
  4. [1, 2, 3, 4, 5] [1, 2, 3, 5]
Answer and explanation

Answer: B. [1, 2, 3, 4] [1, 2, 3, 5]

Explanation: a + [4] creates a brand new list and a is rebound to it, so b still refers to the original [1, 2, 3]. b += [5] then extends that original list in place, which no longer affects a.

Previous: A two-element slice of a list is replaced by a one-element list. What is… · Next: Even numbers are removed from a list while a for loop is running over it. What…

Practise lists & tuples

Try a fresh set of lists & tuples questions with an explanation after every answer, free and without signing up: Lists & Tuples practice. Or sit a full placement mock test.

More lists & tuples questions

More python practice

Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge