fromkeys() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The fromkeys() is Python's inbuilt method of dictionaries. This method creates a new dictionary from the given sequence of elements with a value provided by the user.

Syntax of fromkeys() function in Python

The syntax of fromkeys() in python is:

fromkeys() is used to create a new dictionary from the given sequence of elements and the values the users provide to create a key-value pair in the dictionary. For more understanding, let's move to the parameter section.

Parameters of fromkeys() function in Python

fromkeys() in python accepts 2 parameters. These parameters are:

  • sequence(mandatory): Either list or tuple whose items are used to form keys of the new dictionary.
  • value(optional): This optional parameter provides a single value against each key of the dictionary. If we don't pass any value parameter in fromkeys(), then its default value is NONE.

Return Value of fromkeys() function in Python

The fromkeys() function in python returns a newly formed dictionary where each item of a sequence is set as a key of the dictionary, and the value parameter is set as the value against each key of the dictionary. And if we don't provide any value, then all value fields are set as NONE.

Example of fromkeys() function in Python

In this example, we are using fromkeys() function in python to create the dictionary from the given input sequence by the user. Here all the items of the sequence which is provided by the user are converted as the keys of the newly formed dictionary. If we don't pass any value parameter to the dictionary, it will automatically set NONE as the value against each key.

Example of fromkeys in python

What is fromkeys() function in Python?

The fromkeys() method creates a new dictionary from the given sequence of elements with a value provided by the user. fromkeys() in python accepts two parameters out of which is mandatory and the other one is an optional parameter. It returns the new dictionary where the items of the sequences are considered as keys of the dictionary. In contrast, values parameters are considered values of each key of the dictionary.

Behavior of fromdict() with Mutable Object as values

fromdict() can also be supplied with the mutable object as the default value. But in this case, a deep copy is made of the dictionary, i.e., if we append a value in the original list, the append takes place in all the values of keys.

Code:

Output:

More Examples

Examle 1: Passing only Sequence Parameter in fromkeys() function

In this example, we are going to use the fromkeys() of python by passing only the sequence parameter to the function.

As we are not using any value, the value of each key of the newly formed dictionary is set to be None.

Code:

fromkeys sequence parameter example

Output:

Example 2: Passing Sequence as well as Value parameter to the fromkeys() function

In this example, we are going to use fromkeys() of python by passing both of the parameters, i.e., sequence and value.

As we are passing a value as a parameter to the function, all the keys are assigned with the same value.

All the items of sequence are stored as keys in the dictionary, and to each key, a value is then assigned. fromkeys bypassing parameters example

Code

Output

Conclusion

  • fromkeys() is used for creating a new dictionary from given values from the user.
  • This function accepts two parameters: ' sequenceis a mandatory parameter while thevalue` is an optional parameter.
  • It returns a newly formed dictionary where each item of a sequence is set as a key of the dictionary, and the value parameter is set as the value against each key of the dictionary.

See Also: