Python dict.fromkeys() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
dict classmethod

Definition

fromkeys() creates a new dict whose keys come from any iterable (a list, tuple, string, or another dict's keys) and whose values are all the same object: None unless you pass a value.

Repeated keys collapse into one entry and keep their first-seen position, which makes list(dict.fromkeys(items)) a short way to remove duplicates while keeping order.

dict.fromkeys() is a classmethod of the built-in dict type: call it on the class itself, as dict.fromkeys(...), and it returns a brand-new dict. No import is needed.

Syntax

Call it on the dict class with the keys and an optional shared value.

python
new_dict = dict.fromkeys(keys)
new_dict = dict.fromkeys(keys, value)
CallWhat it does
dict.fromkeys(["a", "b"])Returns {'a': None, 'b': None}.
dict.fromkeys("xyz", 0)Returns {'x': 0, 'y': 0, 'z': 0}.
dict.fromkeys([3, 1, 3, 2])Returns {3: None, 1: None, 2: None}: duplicates collapse, first-seen order is kept.

Calling it on an existing dict, such as prices.fromkeys(keys), also works but ignores that dict's contents; writing dict.fromkeys() makes the intent clear.

Method signatures
python
dict.fromkeys(iterable, value=None, /)
# classmethod; returns: a new dict mapping every key to value

Parameters

ParameterDescription
iterableThe keys: any iterable of hashable values. A string gives one key per character. An unhashable item raises TypeError.
valueOptional. The one value given to every key. Defaults to None.

Return Value

Return ValueDescription
dictA brand-new dict. The iterable you passed is not changed.

Python Examples

Example 1 — Start an Attendance Sheet with False for Everyone

Mark every student absent at first, then record one arrival.

python
students = ["Maya", "Leo", "Iris"]
attendance = dict.fromkeys(students, False)
print(attendance)

attendance["Leo"] = True
print(attendance)
print(sum(attendance.values()), "present")
print(dict.fromkeys("abc"))

How It Works

  1. One value for all. Each name from the list becomes a key, and every key starts as False.
  2. Replacing is safe. Assigning True to Leo puts a new object under one key; the other keys keep False.
  3. Booleans add up. True counts as 1 in sum(), so the sheet reports 1 present.
  4. String keys, default value. A string is iterated character by character, and leaving out the value gives every key None.

Example 2 — See Why fromkeys(keys, []) Shares One List

Plan tasks per weekday the wrong way and the right way.

python
days = ["mon", "tue", "wed"]
tasks = dict.fromkeys(days, [])
tasks["mon"].append("gym")
print(tasks)
print(tasks["mon"] is tasks["wed"])

tasks = {day: [] for day in days}
tasks["mon"].append("gym")
print(tasks)

How It Works

  1. One list, three keys. fromkeys() stores the same empty list under mon, tue, and wed.
  2. Mutation shows everywhere. Appending "gym" through mon changes that single list, so all three days print it.
  3. Proof with is. tasks["mon"] is tasks["wed"] is True: both keys hold the identical object.
  4. Comprehension fix. {day: [] for day in days} evaluates [] once per key, so only Monday gets the task.

Example 3 — Remove Duplicate Tags and Keep Their Order

Use the keys of fromkeys() as an ordered set, then reuse them as counters.

python
tags = ["python", "web", "python", "api", "web", "flask"]
unique = list(dict.fromkeys(tags))
print(unique)
print(len(tags), "->", len(unique))

counts = dict.fromkeys(unique, 0)
for tag in tags:
    counts[tag] += 1
print(counts)

How It Works

  1. Duplicates collapse. The second "python" and "web" hit keys that already exist, so they add nothing new.
  2. First-seen order. Unlike set(), the keys stay in the order the tags first appeared, and list() turns them back into a list.
  3. Shared zero is fine. Every counter starts as the same 0, but += 1 stores a new integer instead of changing the shared one.
  4. Final tally. python and web appear twice; api and flask once.

Frequently Asked Questions

It builds a new dict whose keys come from an iterable and whose values are all the same value, None by default. For example dict.fromkeys(["a", "b"], 0) gives {"a": 0, "b": 0}.
dict.fromkeys(keys, []) stores one shared list under every key. Use a comprehension such as {k: [] for k in keys} to create a separate list per key.
Use list(dict.fromkeys(items)). Dict keys are unique and keep insertion order, so the first occurrence of each item is kept.
Call it on the class, as dict.fromkeys(...). It is a classmethod, so calling it on an existing dict ignores that dict and still returns a new one.
None. dict.fromkeys(["x", "y"]) returns {"x": None, "y": None}.

Next: dict.get()

Return the value for a key, or a default instead of raising KeyError.

get() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful