Map Numbers to Their Squares
You are given a list of numbers and need to build a dictionary where each number is used as a key and its square is used as the value.
This is different from returning a list of squared numbers. The output should be a mapping that lets you look up a number and immediately see its square. So if the input contains 3, the dictionary should contain the pair 3: 9. If the input contains -2, the dictionary should contain -2: 4.
For example, if the input is [1, 2, 3], the correct output is {1: 1, 2: 4, 3: 9}. If the input is [0, -2], the output is {0: 0, -2: 4}. An empty input list should produce an empty dictionary.
The output shape is the main detail to watch. The result should not be [1, 4, 9] or [0, 4]. It should be a dictionary that connects each original number to its squared result.
So the task is to turn the input numbers into key-value pairs of the form number -> number squared.
Example Input & Output
The dictionary maps each number to its square, including negative inputs.
Each number becomes a key, and its square becomes the stored value.
A single input value still produces a one-entry dictionary.
Algorithm Flow

Solution Approach
This problem is a great match for a Python dictionary comprehension. A dictionary comprehension lets us build key-value pairs in one readable expression, which is exactly what this task asks for.
The rule is simple: for each number in the input list, use that number as the key and use its square as the value. In Python, the most direct version is:
This says: iterate through items, and for every num, store one entry where the key is num and the value is num * num.
The nice part is that the code matches the problem statement almost word for word. We are not building the dictionary in several manual steps, and we are not storing unnecessary intermediate structures.
One detail to remember is what happens if the same number appears multiple times. In a Python dictionary, keys are unique, so later entries with the same key overwrite earlier ones. In this problem that is harmless, because the square of a number is always the same. So repeated input values do not create conflicting data.
You could also solve this with a regular loop by creating an empty dictionary and assigning result[num] = num * num for every input value. That version is fine, especially for beginners. But the dictionary comprehension is more idiomatic because the whole task is simply a mapping from one form to another.
This solution also keeps the output shape front and center. That matters because this problem can be misread as "return the squared numbers" when the actual goal is "return a lookup table from numbers to squares." The comprehension helps reinforce that by explicitly writing both the key and the value.
The runtime is O(n) because we visit each input value once. The extra space is also O(n) in the worst case if all numbers are distinct.
Best Answers
def create_dict(items):
result = {}
for x in items:
result[x] = x * x
return resultComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
