Courseiva
Functions, Tuples, Dictionaries and ExceptionshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A server logs are stored as a list of tuples: `logs = [('2024-01-10', 'INFO', 'Started'), ('2024-01-10', 'ERROR', 'Disk full')]`. A developer wants to count how many ERROR logs exist. Which code snippet correctly counts them?

⚠ Common exam trap

Python Institute often tests the distinction between `list.count()` (which requires an exact match of the entire element) and counting via a conditional expression with `sum()`, leading candidates to mistakenly think `count()` can filter by a partial tuple or a specific field.

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

count = sum(log[1] == 'ERROR' for log in logs)

Uses a generator expression with `sum()` to count how many tuples in the `logs` list have the second element equal to `'ERROR'`. The expression `log[1] == 'ERROR'` evaluates to `True` (which is treated as 1) or `False` (0) for each tuple, and `sum()` adds them up, giving the correct count of ERROR logs.

Answer analysis

Option-by-option breakdown

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

  • count = logs.count(('ERROR',))

    Why it's wrong here

    count() does not work with partial tuple matching.

  • count = sum(log[1] == 'ERROR' for log in logs)

    Why this is correct

    Sum of booleans gives the count.

  • count = [log for log in logs if log[1] == 'ERROR']

    Why it's wrong here

    This creates a list, not a count.

  • count = len(logs)

    Why it's wrong here

    This counts all logs, not just errors.

About these practice questions

Courseiva writes every PCEP question from scratch — 498 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 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.