What does {w: len(w) for w in ['hi', 'cat']} produce?
Correct answer
C. {'hi': 2, 'cat': 3}
19 views
C. {'hi': 2, 'cat': 3}
19 views
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.
Asked in: NTS, PPSC, Python Developer Interviews
Not sure why the answer is C? Ask here and get an explanation.