The dict.fromkeys() method creates a new dictionary from a sequence of keys, giving every key the same value. It is handy for setting up counters, flags or default settings in one line. This page explains the syntax with outputs, shows the one trap every beginner falls into (mutable values), and covers two useful tricks: removing duplicates and building a dictionary from two lists.
Syntax
dict.fromkeys(iterable, value=None)
iterable: the keys (a list, tuple, string, set and so on).value: the value given to every key. If you leave it out, it isNone.- It returns a new dictionary; nothing existing is changed.
Basic Examples
keys = ['a', 'b', 'c']
print(dict.fromkeys(keys, 0))
print(dict.fromkeys(keys))
print(dict.fromkeys('xyz', 1))
Output:
{'a': 0, 'b': 0, 'c': 0}
{'a': None, 'b': None, 'c': None}
{'x': 1, 'y': 1, 'z': 1}
In the last line, a string is iterable, so each character becomes a key.

A Practical Use: Setting Up Attendance
students = ['Riya', 'Aman', 'Kabir']
attendance = dict.fromkeys(students, 'Absent')
attendance['Aman'] = 'Present'
print(attendance)
Output:
{'Riya': 'Absent', 'Aman': 'Present', 'Kabir': 'Absent'}
The Trap: Using a List (or Any Mutable Value)
fromkeys() does not make a copy of the value for each key. Every key points to the same object. With numbers and strings this does not matter, but with a list it causes a surprising bug:
d = dict.fromkeys(['a', 'b', 'c'], [])
d['a'].append(1)
print(d)
Output:
{'a': [1], 'b': [1], 'c': [1]}
Adding to d['a'] changed all three, because they share one list. Use a dictionary comprehension instead, which creates a fresh list for each key:
d = {k: [] for k in ['a', 'b', 'c']}
d['a'].append(1)
print(d)
Output:
{'a': [1], 'b': [], 'c': []}
Keys and Values from Two Lists? Use zip(), Not fromkeys()
If you pass a list as the value, every key gets the whole list:
keys = ['a', 'b', 'c']
values = [10, 20, 30]
print(dict.fromkeys(keys, values))
print(dict(zip(keys, values)))
Output:
{'a': [10, 20, 30], 'b': [10, 20, 30], 'c': [10, 20, 30]}
{'a': 10, 'b': 20, 'c': 30}
zip() pairs the items one by one, and dict() turns those pairs into a dictionary.

Trick: Remove Duplicates and Keep the Order
Dictionary keys are unique and, since Python 3.7, keep their insertion order. So fromkeys() is a neat way to remove duplicates from a list without changing the order:
items = ['apple', 'banana', 'apple', 'cherry', 'banana']
print(list(dict.fromkeys(items)))
Output:
['apple', 'banana', 'cherry']
Converting to a set also removes duplicates, but a set does not keep the original order.
Frequently Asked Questions
What is the default value in fromkeys()?
None.
Does fromkeys() change an existing dictionary?
No. It is a class method that always returns a new dictionary.
Why did all my keys change when I updated one list?
Because fromkeys() gives every key the same list object. Use {k: [] for k in keys} instead.
Related: Python dictionary setdefault() · Python list sort()