LearnFrenzy
Sign in Take a free test

Lists · Predict the Output · Question 9

+= and + on an aliased list Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

What is the output of the following Python program?

main.py
a = [1]
b = a
a += [2]
print(b)
a = a + [3]
print(a, b)
  • A [1, 2] [1, 2, 3] [1, 2] Correct
  • B [1] [1, 2, 3] [1]
  • C [1, 2] [1, 2, 3] [1, 2, 3]
  • D [1, 2] [1, 3] [1, 2]
Correct Answer: Option A

+= extends the shared list in place, so b sees 2. a + [3] creates a new list and rebinds only a.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links