LearnFrenzy
Sign in Take a free test

Sets · Find the Error · Question 6

Bug: unique visits come out in the wrong order Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

The pages should be listed in the order they were first visited: 5, 3, 1. Which statement correctly identifies the bug?

main.py
visits = [5, 3, 5, 1, 3]
unique = list(set(visits))
print(unique)
  • A list() sorts its argument
  • B A set does not keep insertion order; use list(dict.fromkeys(visits)) to remove duplicates in order Correct
  • C set() removes the wrong items
  • D visits must be a tuple
Correct Answer: Option B

Small ints happen to come out ascending here; strings may come out in any order. Never rely on set order.

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