Explore Library
Code QuizAdvanced

Dict vs Set Comprehension

Spot why this comprehension meant to build a lookup dict breaks on indexing.

Codepython
nums = [1, 2, 3, 4]
# Goal: build a dict mapping each number to its square
squares = {n * n for n in nums}

print(squares[2])  # expecting 4

The code should map each number to its square and look up the square of 2, but it fails. What is the bug?