LearnFrenzy
Sign in Take a free test

Lists · Predict the Output · Question 6

Slice assignment Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following Python program?

main.py
a = [1, 2, 3, 4, 5]
a[1:3] = [9]
print(a)
a[::2] = [0, 0]
print(a)
  • A [1, 9, 4, 5] [0, 9, 0, 5] Correct
  • B [1, 9, 3, 4, 5] [0, 9, 0, 4, 0]
  • C [1, 9, 4, 5] ValueError
  • D [1, [9], 4, 5] [0, [9], 0, 5]
Correct Answer: Option A

Replacing two items with one shrinks the list to [1, 9, 4, 5]. a[::2] then selects indexes 0 and 2 – exactly two items – so two values fit.

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