Python Dictionary fromkeys() Method With Examples

Python Dictionary fromkeys

Now, we are going to learn about the fromkeys method in relation to the dictionary. Using the fromkeys method, we can create a new dictionary, with the given iterables, and the value. The iterable specifies the keys, and the value corresponding to each key is the value that is provided. If no value is provided, then the value is None, for all the keys. Let’s have an example for a better understanding of the fromkeys method –

Python Dictionary fromkeys() Method

As you can see in the above program, we have a list, which has the keys for the dictionary, and we also have the value specified, which is going to be the value for each key in the dictionary.

If you try to run the above program, the output you get, is a dictionary. It looks something like this –

{‘key1’: 10, ‘key2’: 10, ‘key3’: 10}

Well, as you can see, the elements from the iterables were taken as keys, and the value corresponding to each key, is the value that was provided to the method. If no value is provided, the default value would be taken, and the default value is None.

So, if you are required to create a new dictionary out of the given keys and the specified value, you can use this method.

But now, you might be wondering that what if each key is having different value, like what if just like the list of keys, we get a list of values as well, what are we going to do at that time? Because, if you have list of values as well, and you simply use the fromkeys method, you will see that each key has now got that list of values, as it’s value. You can even try this. You just have to have the value as a list as well. Let’s try this as well.

As you can see in the above program, we have a list of keys, and then we also have a list of values. But the thing is that if we are directly using fromkeys method here, the keys would be like the previous output, but the corresponding value would be the list, for each key. This is the output that we get here.

{‘key1’: [10, 20, 30], ‘key2’: [10, 20, 30], ‘key3’: [10, 20, 30]}

As you can see, the output is not as expected. We wanted to have one key, and one value corresponding to it. To achieve this, we need to use a function, which is zip function. So, just like the previous program, Let’s say that we again have the list of keys and the values, and then we try to use the zip function.

Have a look at the below program, where we are doing what we discussed just above –

As you can see in the above program, we have made use of the zip function. Basically, this zip function is used to yield tuples, till an input is exhausted.

If you try to have a look at the output now, you can find that the keys and values are given accordingly, like the key1 has a value of 10, and so on.  Have a look at the output now –

{‘key1’: 10, ‘key2’: 20, ‘key3’: 30}

As you can see, the output is as we had expected. Remember that we had to use the zip function to achieve this. So, as and when required, we can make use of the fromkeys method in our program. Remember that with the fromkeys method, we are able to create a new dictionary, with the specified keys from the iterable, and the value set to the given value.