Python 3 – dictionary fromkeys() Method
In Python, dictionaries are one of the most useful data structures that you can use to store data in a key-value format. The dictionary fromkeys() method is one of the built-in methods in Python that can be used to create a new dictionary with keys from a sequence and values all set to the same default value.
In this article, we will explore the dictionary fromkeys() method in depth and learn how to use it with the help of some examples.
Syntax of the dictionary fromkeys() Method
The syntax of the dictionary fromkeys() method is as follows:
dictionary.fromkeys(keys, value)
Where,
- keys: A sequence of elements that will be used as keys in the dictionary.
- value (optional): The default value that will be set for all keys in the dictionary. If this argument is not provided, the default value will be None.
Examples
Let’s take a look at some examples to understand how to use the dictionary fromkeys() method:
Example 1: Using the dictionary fromkeys() method with a List of Keys
In this example, we will use a list of keys and the dictionary fromkeys() method to create a new dictionary.
keys = ['apple', 'banana', 'orange']
values = 0
fruits_dict = dict.fromkeys(keys, values)
print(fruits_dict)
Output:
{'apple': 0, 'banana': 0, 'orange': 0}
Explanation:
In the above code, we have created a list of keys ‘apple’, ‘banana’, and ‘orange’, and a variable called values, which will be set as a default value for all the keys in the dictionary. Then, we have used the dictionary fromkeys() method to create a new dictionary with these keys and this default value, and we have printed the resulting dictionary to the console.
Example 2: Using the dictionary fromkeys() method with a Tuple of Keys
In this example, we will use a tuple of keys and the dictionary fromkeys() method to create a new dictionary.
keys = ('red', 'green', 'blue')
value = [0, 1, 2]
colors_dict = dict.fromkeys(keys, value)
print(colors_dict)
Output:
{'red': [0, 1, 2], 'green': [0, 1, 2], 'blue': [0, 1, 2]}
Explanation:
In this example, we have created a tuple of keys ‘red’, ‘green’, and ‘blue’ and a list of values [0, 1, 2]. We have then used the dictionary fromkeys() method to create a new dictionary using these keys and this default value, and we have printed the resulting dictionary to the console.
Example 3: Using the dictionary fromkeys() method without Providing the Value Parameter
In this example, we will use the dictionary fromkeys() method without providing the value parameter.
keys = ('dog', 'cat', 'bird')
animals_dict = dict.fromkeys(keys)
print(animals_dict)
Output:
{'dog': None, 'cat': None, 'bird': None}
Explanation:
In this example, we have created a tuple of keys ‘dog’, ‘cat’, ‘bird’ and we have used the dictionary fromkeys() method to create a new dictionary without providing the value parameter. In this case, the default value for all keys is None, and we have printed the resulting dictionary to the console.
Conclusion
The dictionary fromkeys() method is a useful built-in method in Python that allows you to create a new dictionary with keys from a sequence and values all set to the same default value. You can use it with a list, tuple, or any other sequence of valid keys. By using this method, you can save a lot of time and effort in creating dictionaries that have a lot of keys with the same default value.