Python Dictionary fromkeys() Method

The fromkeys() method in Python is a built-in method used to create a new dictionary with specified keys and values. The method takes a list of keys and an optional value, and it returns a new dictionary with the specified keys and values.

Syntax

dict.fromkeys(keys, value)

Parameters

keys: This parameter is required and it specifies the keys of the new dictionary. It can be a list, tuple, or any iterable object that contains the keys of the new dictionary.

value (optional): This parameter is optional and it specifies the value that will be assigned to all the keys in the new dictionary. The default value is None.

Examples

Example 1: Creating a Dictionary with Specified Keys and Values

Let’s consider a list called keys, and we will use the fromkeys() method to create a new dictionary with the specified keys and values:

keys = ['name', 'age', 'gender']
student = dict.fromkeys(keys, 'unknown')
print(student)

In this example, we have created a list called keys, which contains the keys of the new dictionary. Then, we have used the fromkeys() method to create a new dictionary, with the specified keys and the value ‘unknown’ for all keys. Finally, we have printed the new dictionary, which will be {'name': 'unknown', 'age': 'unknown', 'gender': 'unknown'}.

Example 2: Creating a Dictionary with Specified Keys Only

Let’s consider the following example, where we create a dictionary with specified keys only:

keys = ['name', 'age', 'gender']
student = dict.fromkeys(keys)
print(student)

In this example, we have created a list called keys, which contains the keys of the new dictionary. Then, we have used the fromkeys() method to create a new dictionary, with the specified keys only. The value for each key is None, which is the default value. Finally, we have printed the new dictionary, which will be {'name': None, 'age': None, 'gender': None}.

Example 3: Modifying the Values in the Dictionary

Let’s consider the following example, where we modify the values in the dictionary created in Example 1:

keys = ['name', 'age', 'gender']
student = dict.fromkeys(keys, 'unknown')
student['name'] = 'John'
student['age'] = 25
student['gender'] = 'Male'
print(student)

In this example, we have created a list called keys, which contains the keys of the new dictionary. Then, we have used the fromkeys() method to create a new dictionary, with the specified keys and the value ‘unknown’ for all keys. After that, we have modified the values of the keys ‘name’, ‘age’, and ‘gender’ in the dictionary. Finally, we have printed the new dictionary, which will be {'name': 'John', 'age': 25, 'gender': 'Male'}.

Conclusion

In conclusion, the fromkeys() method in Python is a useful tool to create a new dictionary with specified keys and values. The method takes a list of keys and an optional value, and it returns a new dictionary with the specified keys and values. The method is simple and easy to use, and it can be useful when you need to create a new dictionary with the same keys and values for all keys. It is important to note that the method does not modify the original dictionary and returns a new one with the specified keys and values.

Leave a Reply

Your email address will not be published. Required fields are marked *