Python .fromkeys()
Published May 23, 2022
The .fromkeys() method returns a dictionary with a specified set of keys. It can also initialize the entries with a specified value.
Syntax
dict.fromkeys(keys,value)
Where keys is an iterable containing the keys for the new dictionary and value is an optional initial value for the new entries in the dictionary. If value is not specified, the default initial value for each key will be None.
Codebyte Example
The following example creates a new dictionary with an initial value for each key.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours