Python Dictionary fromkeys() Method

dict.fromkeys() creates a new dictionary from a group of keys.

It is useful when you already know the keys you want, and every key should start with the same value.

One important beginner mistake to watch for is using a mutable value like a list or dictionary. In that case, all keys share the same object.

Quick example

keys = ["name", "age", "city"]
person = dict.fromkeys(keys, "unknown")
print(person)
# {'name': 'unknown', 'age': 'unknown', 'city': 'unknown'}

Use dict.fromkeys(iterable, value) to create a new dictionary where each key starts with the same value.

What dict.fromkeys() does

dict.fromkeys():

  • Creates a new dictionary from a sequence of keys
  • Assigns the same value to every key
  • Returns a new dictionary
  • Is called as dict.fromkeys(...), not on an existing dictionary

This is a class method on dict, so you write dict.fromkeys(...) instead of something like my_dict.fromkeys(...).

If you need a refresher on dictionaries first, see Python dictionaries explained.

Syntax and parameters

dict.fromkeys(iterable, value)

Parameters

  • iterable: the keys to use
  • value: optional starting value for every key

If you leave out value, each key gets None.

Return value

It returns a new dictionary.

Basic example

Here is a simple example using a list of strings as keys:

keys = ["red", "green", "blue"]
colors = dict.fromkeys(keys, 0)

print(colors)

Output:

{'red': 0, 'green': 0, 'blue': 0}

What happens here:

  • keys contains the dictionary keys
  • 0 is used as the starting value for every key
  • dict.fromkeys() returns the new dictionary

If you want to learn more ways to create dictionaries, see creating a dictionary in Python.

Using fromkeys() without a value

If you do not pass the second argument, the default value is None.

fields = ["username", "email", "password"]
user_data = dict.fromkeys(fields)

print(user_data)

Output:

{'username': None, 'email': None, 'password': None}

This is useful when:

  • You want to create the keys first
  • You plan to fill in the real values later
  • You need placeholder values during setup

Example:

fields = ["username", "email", "password"]
user_data = dict.fromkeys(fields)

user_data["username"] = "alice"
user_data["email"] = "alice@example.com"

print(user_data)

Output:

{'username': 'alice', 'email': 'alice@example.com', 'password': None}

If you want to add or change keys later, see how to add a key to a dictionary in Python.

Important warning about mutable values

Be careful when using a mutable value such as:

  • [] (list)
  • {} (dictionary)
  • set() (set)

All keys will point to the same object.

Example of the problem

keys = ["a", "b", "c"]
data = dict.fromkeys(keys, [])

data["a"].append(1)

print(data)

Output:

{'a': [1], 'b': [1], 'c': [1]}

Many beginners expect only "a" to change, but all keys change because they share the same list.

Why this happens

dict.fromkeys(keys, []) creates one list object.

That same list is then used for every key.

So this:

data["a"].append(1)

changes the shared list, not a separate list just for key "a".

The better way when each key needs its own list

Use a dictionary comprehension:

keys = ["a", "b", "c"]
data = {key: [] for key in keys}

data["a"].append(1)

print(data)

Output:

{'a': [1], 'b': [], 'c': []}

Now each key has its own separate list.

This same rule applies if you want each key to have its own dictionary or set.

When to use fromkeys()

dict.fromkeys() is a good choice when:

  • You want to quickly create a dictionary with known keys
  • Every key should start with the same simple value
  • You need default flags, counters, or placeholders

Examples:

flags = dict.fromkeys(["is_admin", "is_active", "is_verified"], False)
print(flags)
scores = dict.fromkeys(["math", "science", "history"], 0)
print(scores)

It is not the best choice when:

  • Each key needs a different starting value
  • Each key needs its own new list or dictionary

fromkeys() vs creating a dictionary manually

fromkeys() is shorter when all keys share the same value.

Using fromkeys()

settings = dict.fromkeys(["sound", "music", "notifications"], True)
print(settings)

Creating a dictionary manually

settings = {
    "sound": True,
    "music": False,
    "notifications": True
}
print(settings)

Manual creation is clearer when the values are different.

Using a comprehension for separate mutable values

keys = ["a", "b", "c"]
data = {key: [] for key in keys}
print(data)

Use:

  • fromkeys() when one shared simple default makes sense
  • Manual dictionary creation when values differ
  • A comprehension when each key needs its own new list or dict

If you later want to change several values at once, you may also want to learn the Python dictionary update() method.

Common mistakes

Here are some common problems beginners run into with dict.fromkeys().

Using a mutable default value by mistake

d = dict.fromkeys(["a", "b"], [])
d["a"].append(1)
print(d)

Output:

{'a': [1], 'b': [1]}

This happens because both keys share the same list.

Use this instead:

keys = ["a", "b"]
d = {key: [] for key in keys}
print(d)

Trying to call fromkeys() on a list

Wrong idea:

keys = ["a", "b", "c"]
# keys.fromkeys(0)

fromkeys() belongs to dict, not list.

Correct usage:

keys = ["a", "b", "c"]
d = dict.fromkeys(keys, 0)
print(d)

Expecting fromkeys() to update an existing dictionary

dict.fromkeys() does not change an existing dictionary. It creates a new one.

original = {"x": 1}
new_dict = dict.fromkeys(["a", "b"], 0)

print(original)
print(new_dict)

Output:

{'x': 1}
{'a': 0, 'b': 0}

If you want to modify an existing dictionary, see the Python dictionary update() method.

Forgetting that the default value is None

d = dict.fromkeys(["x", "y"])
print(d)

Output:

{'x': None, 'y': None}

If you do not pass a second argument, every key gets None.

Useful checks

You can run these small examples to understand how fromkeys() works.

print(dict.fromkeys(["a", "b"], 0))
d = dict.fromkeys(["x", "y"])
print(d)
d = dict.fromkeys(["a", "b"], [])
d["a"].append(1)
print(d)
print(type(dict.fromkeys([1, 2, 3], "value")))

FAQ

Does fromkeys() change an existing dictionary?

No. It creates and returns a new dictionary.

What value is used if I do not pass the second argument?

Each key gets None.

Can I use a list as the default value?

Yes, but all keys will share the same list object.

How do I give each key its own empty list?

Use a dictionary comprehension:

keys = ["a", "b", "c"]
data = {key: [] for key in keys}

See also

Use dict.fromkeys() when all keys should start with the same simple value. If each key needs its own separate list or dictionary, use a dictionary comprehension instead.