Q:

Initialize dictionary with default values using python programming

belongs to collection: Python List Exercises

0

Initialize dictionary with default values

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}}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

Use the fromkeys() method of dict.

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"])

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write a Python program to create a new dictionary ... >>
<< Given a Python list, write a program to remove all...