PCEP Computer Programming and Python Fundamentals Practice Question
A Python program is designed to process user input and store results in a dictionary. The code uses the statement: my_dict[user_key] = value. Under which condition will this statement raise a TypeError?
⚠ Common exam trap
Python Institute often tests the distinction between mutable and immutable types as dictionary keys, trapping candidates who think any object can be a key or that duplicate keys cause errors.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
If the key is a list.
Dictionary keys must be immutable (hashable) types. A list is mutable and therefore unhashable, so using it as a key in a dictionary assignment raises a TypeError. The statement `my_dict[user_key] = value` will fail at runtime if `user_key` is a list.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
If the key is None.
Why it's wrong here
None is hashable, so using it as a key succeeds and stores the entry; TypeError occurs only for unhashable types like lists or dicts. It is tempting because None often signals missing input, but Python raises no type error for it as a dictionary key.
- ✗
If the key already exists and you try to assign a different value.
Why it's wrong here
Reassigning an existing key simply overwrites its value, which is valid dictionary behaviour and raises nothing. It is tempting because overwriting data silently can be a logic bug, but Python permits it; TypeError requires an unhashable key, not a duplicate one.
- ✗
If the key does not already exist in the dictionary.
Why it's wrong here
Assignment to a new key inserts the item, so a missing key never raises TypeError. The statement fails only when user_key is an unhashable type, such as a list or dict. This option confuses insertion behaviour with hashing, which is the actual constraint on dictionary keys.
- ✓
If the key is a list.
Why this is correct
Dictionary keys must be hashable, and lists are mutable, so Python raises TypeError: unhashable type: 'list' when a list is used as a key. Tuples, strings and integers are hashable and would work, but a list cannot be hashed.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 482-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.