asked 81.0k views
4 votes
What code would you insert into the commented line to obtain the output that reads:

a
b
c

Code:

dct =
lst = ['a','b','c','d']
for i in range(len(lst) - 1):
→dct[lst[i]] = ( lst[i], )
for i in sorted( ()):
→k = dct[i]
→# insert your code

1 Answer

1 vote

Final answer:

To print 'a b c ', insert 'print(k[0], end=' ')' on the commented line. This code accesses the first element of the tuple for each sorted key and prints it on the same line separated by spaces.

Step-by-step explanation:

To obtain the output that reads a b c, you need to print the first element of the tuple that is stored as the value for each key in the dictionary. You can do this by looping over the keys and accessing the values. Here's what you should insert on the commented line to achieve the desired output:

print(k[0], end=' ')

The print statement is used with end=' ' to ensure that a space, rather than a new line, is printed after each element, resulting in all elements being printed on the same line.

Taking a look again at the code:

dct = {}
lst = ['a','b','c','d']
for i in range(len(lst) - 1):
dct[lst[i]] = (lst[i], )
for i in sorted(dct):
k = dct[i]
print(k[0], end=' ')

answered
User Carte
by
7.6k points