In Python, the dictionary method fromkeys generates a new dictionary where each key is associated with the value provided in the 'value' argument. This method does not modify the original dictionary; rather, it constructs a fresh dictionary following the order of the specified keys.
Syntax:
The syntax for the dictionary method fromkeys is presented as follows:
dict.fromkeys(keys, value)
Parameters:
- Keys (required): This parameter represents the keys to be transformed into a dictionary.
- Value (optional): It represents the value for all your keys. The default value is set to None.
Note: The value that you specify in the value parameter is assigned to all the keys of the dictionary.
This function generates a dictionary containing keys. In cases where the keys correspond to none, it yields no value; however, if the keys are associated with a specific value, it returns the corresponding value assigned to that field.
Examples of Dictionary fromkeys
Let us now examine several instances of the fromkeys method utilized with dictionaries.
Example: Working of the fromkeys method
This illustration demonstrates the functionality of the fromkeys method in Python.
Example
# given set of keys
keys = {'a', 'e', 'i', 'o', 'u'}
# creating a dictionary using fromkeys()
dict-1 = dict.fromkeys(keys) # not specified any value
print("Dictionary 1 (without values):", dict-1) # set default values to none
# initializing a variable to store a value
value = 'vowel'
dict-2 = dict.fromkeys(keys, value) # specifying a default value
print("Dictionary 2 (with default value):", dict-2)
Output:
Dictionary 1 (without values): {'o': None, 'i': None, 'a': None, 'e': None, 'u': None}
Dictionary 2 (with default value): {'o': 'vowel', 'i': 'vowel', 'a': 'vowel', 'e': 'vowel', 'u': 'vowel'}
Explanation:
In this instance, we have utilized the fromkeys function to generate a dictionary based on the specified set of keys. As we have not assigned any values in this initial scenario, the default values for the keys in the constructed dictionary are established as None.
Nonetheless, when we created a variable and utilized it as the default value in the fromkeys method, we generate a fresh dictionary that contains the specified keys along with the assigned default value.
Example: Example to show the working of the fromkeys method with mutable objects
This illustration demonstrates the functionality of the fromkeys method when utilized with mutable objects in Python.
Example
# given set of keys
keys = {'p', 'q', 'r', 's', 't'}
# creating a list
list-1 = [2, 5]
# creating a dictionary using fromkeys()
dict-1 = dict.fromkeys(keys, list-1) # used list as default value
print("Dictionary 1 (with Default value as a List):", dict-1)
# updating the list 1
list-1.append(9)
print("Updated List 1:", list-1)
print("Dictionary 1 (with Updated List as Values):", dict-1) # values in dictionary updated
# ---------------------------
print()
# creating another list
list-2 = [3, 7]
# creating a dictionary using dictionary comprehension
dict-2 = {key: list(list-2) for key in keys}
print("Dictionary 2 (with Default value as a List):", dict-2)
# updating the list 2
list-2.append(8)
print("Updated List 2:", list-2)
print("Dictionary 2 (with Updated List as Values):", dict-2) # no change in values
Output:
Dictionary 1 (with Default value as a List): {'s': [2, 5], 'p': [2, 5], 't': [2, 5], 'r': [2, 5], 'q': [2, 5]}
Updated List 1: [2, 5, 9]
Dictionary 1 (with Updated List as Values): {'s': [2, 5, 9], 'p': [2, 5, 9], 't': [2, 5, 9], 'r': [2, 5, 9], 'q': [2, 5, 9]}
Dictionary 2 (with Default value as a List): {'s': [3, 7], 'p': [3, 7], 't': [3, 7], 'r': [3, 7], 'q': [3, 7]}
Updated List 2: [3, 7, 8]
Dictionary 2 (with Updated List as Values): {'s': [3, 7], 'p': [3, 7], 't': [3, 7], 'r': [3, 7], 'q': [3, 7]}
Explanation:
In the preceding illustration, a list has been utilized as the value within the dictionary. Mutable objects refer to elements that are capable of being altered, such as lists, dictionaries, and similar data structures.
In this instance, it is evident that when we utilized the append method to modify the list, the keys were also refreshed with the new values. This occurs due to the fact that every element is referencing the same memory address.
Conclusion
In Python, the fromkeys method creates a dictionary by associating a sequence of keys with corresponding values. If the user omits any specification for the value parameter, the default assignment for the keys is None. This functionality is particularly advantageous when you need to set up your dictionary with initial default values.
Python Dictionary fromkeys Method FAQs
- What is the function of the fromkeys method in Python?
The fromkeys function generates a new dictionary in which a specified value is assigned to each key in the dictionary.
- What is the syntax for fromkeys?
The syntax for the fromkeys method within a Python dictionary is outlined below:
dict.fromkeys(keys, value)
Parameters:
- Keys (mandatory): This parameter signifies the keys that will be converted into a dictionary.
- Value (optional): This indicates the value that will be assigned to each of your keys. By default, it is initialized to None.
This function produces a dictionary where each key corresponds to a value. If any keys are associated with a value of None, the function yields no output for those keys; otherwise, it delivers the specific value assigned to that particular field.
- Is it possible for fromkeys to take any iterable as keys?
Indeed, the fromkeys function is capable of taking any iterable as an argument, such as lists, tuples, sets, or even strings.
- Does fromkeys modify the current dictionary?
No, this approach does not modify the existing direction; rather, it creates a new dictionary that associates a shared value with all its keys.
- Could you provide a straightforward illustration of fromkeys?
Let's see a simple example of fromkeys:
Example
keys = ["name", "age", "city"]
new-dict = dict.fromkeys(keys, "C# Tutorial, 20, Noida")
print(new-dict)
Output:
{'name': 'John'}