1. Home
  2. Data Science
  3. Python
  4. What does {w: len(w) for w in ['hi', 'cat']} produce?

Added 2 Nov 2026

What does {w: len(w) for w in ['hi', 'cat']} produce?

  1. {2: 'hi', 3: 'cat'}
  2. {2, 3}
  3. {'hi': 2, 'cat': 3} Correct answer
  4. [2, 3]

Correct answer

C. {'hi': 2, 'cat': 3}

19 views

MCQSEXAM

Explanation

This is a dictionary comprehension: each word becomes a key and its length the value. Square brackets would make a list, and braces without a colon make a set.

Reference

Asked in: NTS, PPSC, Python Developer Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs