Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer writes a function that retrieves a value from a dictionary and handles missing keys:

def get_value(data, key):
    try:
        return data[key]

except KeyError:

return None

What happens if data is not a dictionary but a list, and key is an integer index?

⚠ Common exam trap

The trap here is assuming that the except KeyError clause will catch any error or that list indexing raises KeyError.

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

✓

The function returns the element at the specified index because lists support integer indexing.

The function uses a try-except block to handle KeyError, which is specific to dictionary key lookups. When data is a list and key is an integer, the expression data[key] performs list indexing, which succeeds if the index is valid. No KeyError is raised, so the except block is skipped, and the element is returned. This shows that the same syntax can work for different types, but the exception handling is tailored to dictionaries.

Answer analysis

Option-by-option breakdown

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

  • ✗

    The function returns None because the except KeyError clause catches any error.

    Why it's wrong here

    The except KeyError clause catches only KeyError exceptions. If data is a list and key is a valid integer index, no exception is raised. Therefore, the function returns the element, not None. If the index were out of range, an IndexError would be raised, which is not caught by except KeyError, so the function would propagate that error.

  • ✗

    The function raises a KeyError because lists use integer indices, not keys.

    Why it's wrong here

    Lists use integer indices with the [] operator, and these are not considered keys in the dictionary sense. A KeyError is raised only when a dictionary is accessed with a missing key. Since data is a list and key is an integer, the operation data[key] performs list indexing and does not raise KeyError. Thus, the function does not raise KeyError.

  • ✗

    The function raises a TypeError because lists do not support the [] operator.

    Why it's wrong here

    Lists do support the [] operator for indexing. When key is an integer, data[key] is valid and returns the element at that index. A TypeError would occur only if the index were not an integer (e.g., a string). The except KeyError clause does not catch TypeError, so if a TypeError were raised, it would propagate. But here no TypeError occurs.

  • ✓

    The function returns the element at the specified index because lists support integer indexing.

    Why this is correct

    Lists support integer indexing, so data[key] with an integer key retrieves the element at that position. The try block succeeds, and the function returns that element. The except KeyError clause is not triggered because no KeyError occurs. Thus, the function works correctly for lists with integer indices, returning the desired element.

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

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

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.