LearnFrenzy
Sign in Take a free test

Sets · Predict the Output · Question 11

Removing duplicates with and without order Answer, explanation & discussion

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

What is the output of the following Python program?

main.py
nums = [3, 1, 3, 2, 1]
print(list(dict.fromkeys(nums)), sorted(set(nums)))
  • A [3, 1, 2] [1, 2, 3] Correct
  • B [1, 2, 3] [1, 2, 3]
  • C [3, 1, 2] [3, 1, 2]
  • D [3, 1, 3, 2, 1] [1, 2, 3]
Correct Answer: Option A

dict.fromkeys keeps the first-seen order; sorted(set(…)) gives the unique values in ascending 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