In Python, we can initialize the keys with the same values.
Given:
employees = ['Kelly', 'Emma'] defaults = {"designation": 'Developer', "salary": 8000}
Expected output:
{'Kelly': {'designation': 'Developer', 'salary': 8000}, 'Emma': {'designation': 'Developer', 'salary': 8000}}
Hint:
Use the fromkeys() method of dict.
fromkeys()
Solution:
The fromkeys() method returns a dictionary with the specified keys and the specified value.
employees = ['Kelly', 'Emma'] defaults = {"designation": 'Developer', "salary": 8000} res = dict.fromkeys(employees, defaults) print(res) # Individual data print(res["Kelly"])
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Hint:
Use the
fromkeys()
method of dict.Solution:
The
need an explanation for this answer? contact us directly to get an explanation for this answerfromkeys()
method returns a dictionary with the specified keys and the specified value.