nerdexam
Python_Institute

PCEP-30-02 · Question #54

Insert the correct snippet to convert the t tuple to a dictionary named d. Expected output: {'A': 1, 'B': 2, 'C': 3} Code: t = (('A', 1), ('B', 2), ('C', 3)) insert code here print(d)

The correct answer is B. d = dict(t). Option B is correct because Python's built-in dict() function accepts an iterable of key-value pairs - exactly what t is: a tuple of 2-element tuples. Each inner tuple ('A', 1) becomes a key-value pair in the resulting dictionary. Why the distractors fail: A (t >> d.dict) - >>…

Question

Insert the correct snippet to convert the t tuple to a dictionary named d. Expected output: {'A': 1, 'B': 2, 'C': 3} Code: t = (('A', 1), ('B', 2), ('C', 3))

insert code here

print(d)

Options

  • At >> d.dict
  • Bd = dict(t)
  • Cd.dict(t)
  • Dd = t(dict)

How the community answered

(20 responses)
  • A
    5% (1)
  • B
    80% (16)
  • C
    10% (2)
  • D
    5% (1)

Explanation

Option B is correct because Python's built-in dict() function accepts an iterable of key-value pairs - exactly what t is: a tuple of 2-element tuples. Each inner tuple ('A', 1) becomes a key-value pair in the resulting dictionary.

Why the distractors fail:

  • A (t >> d.dict) - >> is a bitwise right-shift operator, not a conversion method, and d doesn't exist yet so d.dict would raise a NameError.
  • C (d.dict(t)) - d hasn't been defined, and even if it had, dictionaries have no .dict() method; this would raise AttributeError.
  • D (d = t(dict)) - this tries to call the tuple t like a function with dict as an argument, which raises TypeError since tuples aren't callable.

Memory tip: Think of dict() as a factory that "zips up" pairs into a dictionary - if your data is already shaped as (key, value) pairs, dict() handles the conversion directly, just like list() or tuple() convert other iterables.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice