Q.Which of the following built-in function/method returns a dictionary ? (A) dict() (B) keys() (C) values() (D) items()
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →A dictionary is returned by the constructor that creates one from scratch or from an iterable of key-value pairs. The answer is (A) dict().
The concept: constructors versus accessor methods
Python's dictionary type offers two kinds of operations. The constructor dict() builds and returns a new dictionary object. The methods keys(), values(), and items() are called on an existing dictionary and return views of its contents, not new dictionaries.
Understanding the return type of each is essential for working with dictionaries correctly.
Examining each option
-
dict()— the dictionary constructorThis is Python's built-in function that creates a dictionary. Called with no arguments, it returns an empty dictionary:
d = dict() # returns {}Called with keyword arguments or an iterable of pairs, it constructs and returns a populated dictionary:
dict(a=1, b=2) # returns {'a': 1, 'b': 2} dict([('x', 10), ('y', 20)]) # returns {'x': 10, 'y': 20}The return type is
<class 'dict'>. -
keys()— returns a view of keysThis is a method called on an existing dictionary. It returns a
dict_keysobject, which is a view (not a dictionary):d = {'a': 1, 'b': 2} k = d.keys() # returns dict_keys(['a', 'b']) type(k) # <class 'dict_keys'>You can iterate over it or convert it to a list, but it is not a dictionary.
-
values()— returns a view of valuesSimilarly, this method returns a
dict_valuesobject:v = d.values() # returns dict_values([1, 2]) type(v) # <class 'dict_values'> ``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.