PCEP-30-02 · Question #42
What code would you insert instead of the comment to obtain the expected output? Expected output: a b c dictionary = {} my_list = ['a', 'b', 'c', 'd'] for i in range(len(my_list) - 1)…
The correct answer is A. print(k[0]). Option A is correct because each dictionary value is a single-element tuple (e.g., ('a',)), created by the trailing comma in (my_list[i], ). To extract the string from inside the tuple, you index it with the integer 0 - so k[0] yields 'a', 'b', and 'c' as required. B (print(k))…
Question
Insert your code here.
Options
- Aprint(k[0])
- Bprint(k)
- Cprint(k['0'])
- Dprint(k["0"])
How the community answered
(61 responses)- A79% (48)
- B3% (2)
- C7% (4)
- D11% (7)
Explanation
Option A is correct because each dictionary value is a single-element tuple (e.g., ('a',)), created by the trailing comma in (my_list[i], ). To extract the string from inside the tuple, you index it with the integer 0 - so k[0] yields 'a', 'b', and 'c' as required.
B (print(k)) is wrong because it prints the whole tuple including parentheses and comma - e.g., ('a',) - which doesn't match the expected output. C and D are both wrong for the same reason: tuples (and all sequences) use integer indices, not string indices, so k['0'] and k["0"] both raise a TypeError at runtime.
Memory tip: When you see a trailing comma like (value, ), that's Python's way of making a single-element tuple - and to unwrap it you always use a numeric index like [0], never a string like ['0'].
Community Discussion
No community discussion yet for this question.