LearnFrenzy
Sign in Take a free test

Sets · Find the Error · Question 3

Error: a list inside a set Answer, explanation & discussion

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

The program below stops with an error. Which statement correctly describes the problem?

main.py
visited = set()
visited.add([2, 3])          # a grid cell
print(visited)
  • A It prints {[2, 3]}
  • B TypeError: lists are unhashable and cannot be set elements; add the tuple (2, 3) Correct
  • C It prints {2, 3}
  • D ValueError
Correct Answer: Option B

Use visited.add((2, 3)). Tuples of numbers are hashable.

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