![]() |
VOOZH | about |
Welcome to the Python Dictionary Manipulation Python Interview Questions Dictionaries are versatile data structures in Python, allowing you to store key-value pairs and perform various operations such as adding, removing, updating, and accessing elements efficiently. These questions will test your understanding of various dictionary manipulation techniques, including methods for adding and removing items, accessing values by keys, checking for key existence, and more. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Letβs explore the world of Python dictionary manipulation together!
pop() method do when used on a Python dictionary?a) Removes and returns the last item of the dictionary
b) Removes and returns the item with the specified key
c) Removes and returns the item at the specified index
d) Removes and returns the first item of the dictionary
Answer: b
Explanation: The pop() method removes and returns the item with the specified key from the dictionary.
a) add()
b) insert()
c) append()
d) None of the above
Answer: d
Explanation: There is no add() or insert() method for dictionaries. New key-value pairs are added using assignment, e.g., my_dict['new_key'] = 'value'.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.get('b', 0)
print(result)
a) 1
b) 2
c) 3
d) 0
Answer: b
Explanation: The get() method returns the value of the specified key. If the key is not found, it returns the default value, which is 0 in this case.
a) keys()
b) get_keys()
c) list()
d) None of the above
Answer: a
Explanation: The keys() method returns a view object that displays a list of all the keys in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.keys()
print(result)
a) [βaβ, βbβ, βcβ]
b) (βaβ, βbβ, βcβ)
c) dict_keys([βaβ, βbβ, βcβ])
d) {βaβ, βbβ, βcβ}
Answer: c
Explanation: The keys() method returns a dict_keys object that contains the keys of the dictionary.
a) values()
b) get_values()
c) list()
d) None of the above
Answer: a
Explanation: The values() method returns a view object that displays a list of all the values in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.values()
print(result)
a) [1, 2, 3]
b) (1, 2, 3)
c) dict_values([1, 2, 3])
d) {1, 2, 3}
Answer: c
Explanation: The values() method returns a dict_values object that contains the values of the dictionary.
a) items()
b) get_items()
c) list()
d) None of the above
Answer: a
Explanation: The items() method returns a view object that displays a list of tuples containing the key-value pairs in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.items()
print(result)
a) [(βaβ, 1), (βbβ, 2), (βcβ, 3)]
b) ((βaβ, 1), (βbβ, 2), (βcβ, 3))
c) dict_items([(βaβ, 1), (βbβ, 2), (βcβ, 3)])
d) {(βaβ, 1), (βbβ, 2), (βcβ, 3)}
Answer: c
Explanation: The items() method returns a dict_items object that contains the key-value pairs of the dictionary as tuples.
a) remove()
b) delete()
c) pop()
d) None of the above
Answer: c
Explanation: The pop() method removes the specified key and returns the corresponding value from the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.pop('b')
print(result)
a) 1
b) 2
c) 3
d) {βaβ: 1, βcβ: 3}
Answer: b
Explanation: The pop() method removes the key βbβ from the dictionary and returns its corresponding value.
a) clear()
b) delete_all()
c) remove_all()
d) None of the above
Answer: a
Explanation: The clear() method removes all items from the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.clear()
print(my_dict)
a) {}
b) {βaβ: 1, βbβ: 2, βcβ: 3}
c) []
d) None
Answer: a
Explanation: The clear() method removes all items from the dictionary, leaving it empty.
a) merge()
b) update()
c) append()
d) extend()
Answer: b
Explanation: The update() method updates the dictionary with elements from another dictionary object or from an iterable of key-value pairs.
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
dict1.update(dict2)
print(dict1)
a) {βaβ: 1, βbβ: 2, βcβ: 4}
b) {βaβ: 1, βbβ: 3, βcβ: 4}
c) {βaβ: 1, βbβ: 3}
d) {βaβ: 1, βbβ: 2, βcβ: 3}
Answer: b
Explanation: The update() method updates dict1 with the key-value pairs from dict2, overwriting values for keys that already exist in dict1.
a) Dictionaries are ordered collections
b) Dictionaries can contain duplicate keys
c) Dictionaries are indexed by integers
d) Dictionaries are immutable
Answer: b
Explanation: Dictionaries in Python can have duplicate keys, but each key is unique within the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.popitem()
print(result)
a) (βaβ, 1)
b) (βbβ, 2)
c) (βcβ, 3)
d) {βaβ: 1, βbβ: 2}
Answer: c
Explanation: The popitem() method removes and returns the last inserted key-value pair from the dictionary.
a) get_or_insert()
b) setdefault()
c) find_or_insert()
d) None of the above
Answer: b
Explanation: The setdefault() method returns the value of the specified key, and if the key does not exist, it inserts the key with the specified default value.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.setdefault('d', 4)
print(result)
a) 1
b) 2
c) 3
d) 4
Answer: d
Explanation: Since βdβ does not exist in my_dict, setdefault() inserts the key βdβ with the value 4 and returns 4.
a) find()
b) get()
c) retrieve()
d) None of the above
Answer: b
Explanation: The get() method returns the value for the specified key in the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = my_dict.get('b', 0)
print(result)
a) 1
b) 2
c) 3
d) 0
Answer: b
Explanation: The get() method returns the value of the specified key. If the key is not found, it returns the default value, which is 0 in this case.
a) check()
b) exists()
c) in operator
d) None of the above
Answer: c
Explanation: The in operator can be used to check if a key exists in a dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = 'a' in my_dict
print(result)
a) True
b) False
c) 1
d) Error
Answer: a
Explanation: The in operator checks if βaβ exists as a key in the dictionary, which it does, so the output is True.
my_dict = {'a': 1, 'b': 2, 'c': 3}
new_dict = my_dict.copy()
new_dict['d'] = 4
print(my_dict)
a) {βaβ: 1, βbβ: 2, βcβ: 3}
b) {βaβ: 1, βbβ: 2, βcβ: 3, βdβ: 4}
c) {βaβ: 1, βbβ: 2, βcβ: 3, βdβ: 4}
d) {βaβ: 1, βbβ: 2, βcβ: 3}
Answer: a
Explanation: The copy() method creates a shallow copy, so modifying new_dict does not affect my_dict.
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = list(my_dict.keys())
print(result)
a) [βaβ, βbβ, βcβ]
b) [βcβ, βbβ, βaβ]
c) [βaβ, 1, βbβ, 2, βcβ, 3]
d) Error: βdict_keysβ object is not subscriptable
Answer: a
Explanation: list(my_dict.keys()) converts the keys of the dictionary to a list.
def deep_update_dict(d1, d2):
for k, v in d2.items():
if k in d1 and isinstance(d1[k], dict) and isinstance(v, dict):
deep_update_dict(d1[k], v)
else:
d1[k] = v
my_dict1 = {'a': 1, 'b': {'c': 2}}
my_dict2 = {'b': {'d': 3}}
deep_update_dict(my_dict1, my_dict2)
print(my_dict1)
a) {βaβ: 1, βbβ: {βcβ: 2, βdβ: 3}}
b) {βaβ: 1, βbβ: {βdβ: 3}}
c) {βaβ: 1, βbβ: {βcβ: 2}}
d) {βbβ: {βcβ: 2, βdβ: 3}}
Answer: a
Explanation: The deep_update_dict function recursively updates my_dict1 with key-value pairs from my_dict2. Since both βbβ in my_dict1 and my_dict2 are dictionaries, it recursively updates βbβ in my_dict1 with the key-value pair βdβ: 3 from my_dict2.
def dict_merger(dict1, dict2):
"""
Merges two dictionaries into a single dictionary.
Arguments:
dict1 -- First dictionary
dict2 -- Second dictionary
Returns:
dict -- Merged dictionary
"""
merged_dict = dict1.copy()
merged_dict.update(dict2)
return merged_dict
# Example usage:
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
result = dict_merger(dict1, dict2)
print(result)
a) {βaβ: 1, βbβ: 3, βcβ: 4}
b) {βaβ: 1, βbβ: 2, βcβ: 4}
c) {βaβ: 1, βbβ: 2}
d) {βbβ: 3, βcβ: 4}
Answer: a
Explanation: The dict_merger function merges dict2 into dict1 using the update method, overwriting values for keys that exist in both dictionaries.
def dict_value_sum(d):
"""
Sums all the values in a dictionary.
Arguments:
d -- Dictionary
Returns:
int -- Sum of all values
"""
return sum(d.values())
# Example usage:
my_dict = {'a': 1, 'b': 2, 'c': 3}
result = dict_value_sum(my_dict)
print(result)
a) 5
b) 6
c) 7
d) 8
Answer: c
Explanation: The dict_value_sum function calculates the sum of all the values in the dictionary my_dict, which is 1 + 2 + 3 = 6.
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict['d'] = 4
print(my_dict)
a) Adds a new key βdβ with value 4 to the dictionary my_dict
b) Adds a new key β4β with value βdβ to the dictionary my_dict
c) Removes the key βdβ from the dictionary my_dict
d) Updates the value of key βdβ to 4 in the dictionary my_dict
Answer: a
Explanation: The code adds a new key βdβ with value 4 to the dictionary my_dict.
my_dict = {'a': 1, 'b': 2, 'c': 3}
del my_dict['d']
print(my_dict)
a) SyntaxError
b) KeyError
c) IndexError
d) No error
Answer: b
Explanation: The code will raise a KeyError because βdβ does not exist as a key in the dictionary my_dict.
my_dict = {'a': 1, 'b': 2, 'c': 3}
keys = my_dict.keys()
print(keys[0])
a) SyntaxError
b) KeyError
c) TypeError
d) IndexError
Answer: c
Explanation: The code will raise a TypeError because the keys() method returns a view object, not a list, so it does not support indexing.
a) my_dict = {'a': 1, 'b': 2, 'c': 3}
b) my_dict = dict(a=1, b=2, c=3)
c) my_dict = dict([('a', 1), ('b', 2), ('c', 3)])
d) my_dict = dict(a=1, b=2, c=3, 'd'=4)
Answer: d
Explanation: The syntax dict(a=1, b=2, c=3, 'd'=4) is not valid because dictionary keys cannot be specified using both the keyword argument syntax (key=value) and the literal syntax ('key': value) in the same expression.
a) Dictionaries are ordered by default, maintaining the order of elements as they are added.
b) Dictionaries are unordered by default, with no guarantee of element order.
c) Dictionaries are ordered only if explicitly specified with the OrderedDict class.
d) Dictionaries are ordered, but the order is randomized for better performance.
Answer: a
Explanation: Since Python 3.7, dictionaries are ordered by default, meaning they preserve the order of elements as they are added. This behavior was guaranteed starting from Python 3.7+.
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
Answer: a
Explanation: Accessing an item in a Python dictionary by its key has an average time complexity of O(1). This is because dictionaries use a hash table implementation, providing constant-time lookup.
Congratulations on completing the Python Dictionary Manipulation MCQs! Dictionaries are essential data structures in Python, offering efficient ways to store and retrieve key-value pairs. By mastering dictionary manipulation techniques, you gain the ability to work with complex data structures and perform tasks such as data retrieval, modification, and validation. Keep practicing and experimenting with Pythonβs dictionary functionalities to become proficient in handling dictionaries within your programs. If you have any questions or want to delve deeper into any topic, donβt hesitate to continue your learning journey. Happy coding!
You can also enroll in our free Python Course Today!
Read our more articles related to MCQs in Python:
My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.
GPT-4 vs. Llama 3.1 β Which Model is Better?
Llama-3.1-Storm-8B: The 8B LLM Powerhouse Surpa...
A Comprehensive Guide to Building Agentic RAG S...
Top 10 Machine Learning Algorithms in 2026
45 Questions to Test a Data Scientist on Basics...
90+ Python Interview Questions and Answers (202...
8 Easy Ways to Access ChatGPT for Free
Prompt Engineering: Definition, Examples, Tips ...
What is LangChain?
What is Retrieval-Augmented Generation (RAG)?
Edit
Resend OTP
Resend OTP in 45s