LearnFrenzy
Sign in Take a free test

Lists · Predict the Output · Question 1

append() and extend() Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

What is the output of the following Python program?

main.py
a = [1, 2, 3]
a.append([4, 5])
b = [1, 2, 3]
b.extend([4, 5])
print(len(a), len(b), a[-1], b[-1])
  • A 5 5 5 5
  • B 4 4 [4, 5] [4, 5]
  • C 5 4 5 [4, 5]
  • D 4 5 [4, 5] 5 Correct
Correct Answer: Option D

append adds the whole list as one item; extend adds 4 and 5 separately.

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