ExamVeda
Login
Home
Dictionary in Python
?

What is the output of the following code:
my_dict = {'a': 1, 'b': 2}
my_dict.popitem()
print(my_dict)

Answer & Solution
Correct Answer: Option C
Code Analysis:

my_dict = {'a': 1, 'b': 2}

1. popitem() Method: The popitem() method removes and returns the last inserted key-value pair. In this case, the last inserted pair is 'b': 2.

2. Updated Dictionary: After removing 'b': 2, the dictionary will contain only 'a': 1.

print(my_dict)

Output: The dictionary now contains {'a': 1}.

Incorrect Options:

A) {'a': 1, 'b': 2}: This would be the original dictionary, but popitem() modifies it.

B) {}: This would be true if both key-value pairs were removed, but only one is removed.

D) {1: 'a', 2: 'b'}: This is incorrect as keys and values are not swapped.

Thus, the correct answer is Option C: {'a': 1}.
Examveda
Question posted by Examveda
Community

Join the Discussion

1 Comment
Antonio Muñoz
Antonio Muñoz 2 years ago
The answer is: {'a': 1}