Lists & Tuples · Question 9 · Medium

One name is a slice copy of the list a and another is a plain assignment. What is printed?

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

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

Explanation: a[:] builds a new list, so appending 4 to b does not touch a. c = a only gives the same list a second name, so appending 5 through c shows up in a.

Previous: The same list goes through sorted() and then sort(). What does this print? · Next: remove() and pop() are used on a queue of token numbers. What does this print?

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