Professional Documents
Culture Documents
BJHB 3-3
BJHB 3-3
Q10. Discuss the advantages of using dictionaries over other data structures in Python.
Dictionaries offer advantages such as fast lookup, easy association between related data, and
flexible structure. They provide efficient retrieval of values based on keys, making them suitable for tasks
like data indexing and mapping.
Q15. Write a Python program that creates an empty dictionary and adds three key-value pairs to it.
my_dict = { }
my_dict['key1'] = 'value1'
my_dict['key2'] = 'value2'
my_dict['key3'] = 'value3'
Q16. Write a Python program that takes a dictionary as input and prints all the keys and their
corresponding values.
Q17. Show how you can change the value associated with a specific key in a dictionary?
To change the value associated with a specific key in a dictionary, you can assign a new value to
that key. For example, my_dict['key'] = new_value will update the value for the key 'key' in the
dictionary my_dict.
Q18. Explain the difference between the pop() and popitem() methods in Python dictionaries.
The pop() method is used to remove a key-value pair from a dictionary based on the specified key. It
returns the value associated with the key. On the other hand, the popitem() method removes the last
key-value pair from the dictionary and returns it as a tuple. This method is useful when the order of
removal doesn't matter.