fromkeys() in Python

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.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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.

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.
Turn Learning into Career Growth
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:

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.

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.