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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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:

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more