Courseiva

When Can Tuples Be Used as Dictionary Keys in Python?

Which TWO of the following statements about tuples in Python are true?

Quick Answer

The answer is that tuples can be used as dictionary keys only when all of their elements are hashable. This is because tuples themselves are immutable sequences, but their hash value is computed from the hash values of their constituent elements; if any element is unhashable—such as a list or another mutable object—the tuple becomes unhashable and cannot serve as a dictionary key. On the Certified Entry-Level Python Programmer PCEP exam, this concept tests your understanding of the interplay between immutability and hashability, often appearing in multiple-choice questions that present tuples containing lists as a common trap. A frequent trick is assuming all tuples are automatically hashable, but the key rule is that hashability depends on the elements, not just the tuple type. For a quick memory tip, remember: "A tuple is only as hashable as its contents—if it holds a list, it cannot be keyed."

⚠ Common exam trap

Python Institute often tests the misconception that 'tuples are immutable' automatically means 'tuples are always hashable' or 'tuples can only contain immutable objects,' leading candidates to incorrectly select options A or D.

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

✓

Tuples can be used as dictionary keys if all elements are hashable.

Option B is correct because a tuple is hashable only when every element it contains is hashable, so a tuple such as (1, 'a') can serve as a dictionary key, while one containing a list cannot. Option E is correct because tuples are immutable sequences: once created, their length and element references cannot be changed, though they support indexing, slicing, concatenation, and repetition. Option A is wrong because tuples containing mutable or unhashable elements, such as ([1, 2],), are not hashable. Option C is wrong because tuples fully support indexing and slicing, e.g., t[0]. Option D is wrong because tuples may contain mutable objects like lists; the tuple itself is immutable, but its elements need not be.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Tuples are always hashable.

    Why it's wrong here

    A tuple is hashable only if every element it contains is hashable, so tuples holding lists or dictionaries raise TypeError. It is tempting because tuples are immutable and commonly used as dictionary keys, but that works solely when all nested elements are themselves hashable.

  • ✓

    Tuples can be used as dictionary keys if all elements are hashable.

    Why this is correct

    Dictionary keys require hashability, and a tuple is hashable when every contained element is hashable. Immutability alone is insufficient; a tuple holding a list cannot serve as a key, so the condition on all elements is the precise constraint.

  • ✗

    Tuples do not support indexing.

    Why it's wrong here

    Tuples are ordered sequences supporting indexing and slicing exactly like lists, so this statement is false. It is tempting because tuples are immutable, which people wrongly conflate with being unindexable; immutability restricts reassignment of elements, not positional access via square brackets.

  • ✗

    Tuples can only contain immutable objects.

    Why it's wrong here

    Tuples can hold mutable objects such as lists, so this is false; immutability applies to the tuple's own structure, not its contents. It is tempting because tuples themselves cannot be altered, but a contained list can still be modified in place without changing the tuple.

  • ✓

    Tuples are immutable sequences.

    Why this is correct

    Tuples cannot be modified after creation: no item assignment, append, or removal. This immutability is the defining property distinguishing tuples from lists, and it also makes them hashable, so they can serve as dictionary keys.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A developer writes a function that takes a tuple as an argument and tries to modify an element inside the tuple. What happens?

easy
  • ✓ A.The code raises a TypeError.
  • B.The tuple is converted to a list automatically.
  • C.The first element is modified successfully.
  • D.The code raises a ValueError.

Why A: Tuples in Python are immutable, meaning their elements cannot be changed after creation. Attempting to modify an element (e.g., `my_tuple[0] = 5`) raises a `TypeError` because the tuple object does not support item assignment. This is a fundamental property of the tuple data type.

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.