Exercise 9: Invert a Dictionary — Possible Solution ==================================================================== student_ids = {"Alice": 1001, "Bob": 1002, "Cleo": 1003, "Dev": 1001} inverted = {} names = list(student_ids.keys()) index = 0 while index < len(names): name = names[index] student_id = student_ids[name] if student_id in inverted: # Collision: this ID already maps to an earlier name. # Deliberate choice — keep the FIRST name seen, ignore later ones. pass else: inverted[student_id] = name index += 1 print(inverted) Output: {1001: 'Alice', 1002: 'Bob', 1003: 'Cleo'} WHY THIS WORKS AS AN ANSWER ------------------------------ Alice and Dev both have ID 1001, so a naive inversion — always doing inverted[student_id] = name with no check — would silently let whichever one is processed LAST overwrite the other, and since Dev comes after Alice in the dictionary, Dev would win by pure accident of insertion order. That's an easy mistake to ship without ever noticing it, since nothing crashes. This solution makes the collision an explicit, deliberate decision instead: "if student_id in inverted" checks whether this ID has already been claimed, and if so, keeps the first name seen (Alice) rather than letting Dev silently overwrite it. Depending on the real requirement, "keep the last one" or "store both names in a list" would be equally valid alternative decisions — the important part is that the choice is made on purpose, not left to whatever order the dictionary happens to iterate in.