Instagram
youtube
Facebook
Twitter

Python Program to Initialize Dictionary with Default Values

A Python Program to Initialize Dictionary with Default Values?

Code Explanation:

Employee List:
A list named employees is created with employee names.

Default Dictionary:
The defaults dictionary holds common default values for all employees.

fromkeys() Method:
dict.fromkeys(employees, defaults) initializes a new dictionary using keys from the employees list, assigning the same default values to each key.

Resulting Dictionary:
employee_dict becomes a dictionary where each employee has the same role and salary as defined in defaults.

Output Display:
The final dictionary is printed showing default values assigned to all employees.

 

Program:

employees = ['Vinay', 'Rashmi', 'Aman']

defaults = {"designation": 'Developer', "salary": 8000}

employee_dict = dict.fromkeys(employees, defaults)

print(employee_dict)