Question 3
Back to Exercise
The program below stops with an error. Which statement correctly describes the problem?
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!