dict.fromkeys()

dict.fromkeys(iterable[, value])
Returns: dict · Updated March 13, 2026 · Dict Methods
dictionaries methods creation

The .fromkeys() method creates a new dictionary with keys taken from an iterable and all values set to the same specified value. This is particularly useful when you need to initialize a dictionary with default values or create a dictionary from a list of keys.

Syntax

dict.fromkeys(iterable[, value])

Parameters

ParameterTypeDefaultDescription
iterableiterableRequiredAn iterable of keys to use for the new dictionary
valueanyNoneThe value to assign to each key. If omitted, defaults to None

Return Value

Returns a new dictionary with the specified keys, all mapped to the same value.

Basic Examples

Creating a dictionary with default None values

Create a dictionary from a list of keys:

keys = ["name", "age", "city"]

result = dict.fromkeys(keys)
print(result)
# {'name': None, 'age': None, 'city': None}

Creating a dictionary with a default value

Initialize all keys with the same starting value:

columns = ["id", "name", "score", "grade"]

# Initialize with 0 as default
scores = dict.fromkeys(columns, 0)
print(scores)
# {'id': 0, 'name': 0, 'score': 0, 'grade': 0}

Using with a string

Strings are iterable, so each character becomes a key:

# Each letter becomes a key with None value
result = dict.fromkeys("aeiou")
print(result)
# {'a': None, 'e': None, 'i': None, 'o': None, 'u': None}

Using with a range

# Create a dictionary with numeric keys
result = dict.fromkeys(range(5), "default")
print(result)
# {0: 'default', 1: 'default', 2: 'default', 3: 'default', 4: 'default'}

Common Patterns

Initializing counters

A common use case is setting up counters for a collection:

items = ["apple", "banana", "apple", "orange", "banana", "apple"]

# Initialize all counts to zero
counts = dict.fromkeys(set(items), 0)

# Now increment
for item in items:
    counts[item] += 1

print(counts)
# {'apple': 3, 'banana': 2, 'orange': 1}

Creating a mutable default structure

Be careful when using mutable objects as defaults:

# This creates the SAME list object for all keys!
wrong = dict.fromkeys(["a", "b", "c"], [])
wrong["a"].append(1)
print(wrong)
# {'a': [1], 'b': [1], 'c': [1]}  # All share the same list!

# Correct approach: use a dict comprehension for mutable defaults
correct = {key: [] for key in ["a", "b", "c"]}
correct["a"].append(1)
print(correct)
# {'a': [1], 'b': [], 'c': []}  # Each key has its own list

Initializing a grid or matrix

# Create a 3x3 grid initialized with zeros
rows, cols = 3, 3
grid = dict.fromkeys((r, c) for r in range(rows) for c in range(cols), 0)

print(grid)
# {(0, 0): 0, (0, 1): 0, (0, 2): 0, 
#  (1, 0): 0, (1, 1): 0, (1, 2): 0, 
#  (2, 0): 0, (2, 1): 0, (2, 2): 0}

Creating flags or boolean states

features = ["auth", "logging", "caching", "compression", "encryption"]

# Initialize all feature flags as disabled
enabled_features = dict.fromkeys(features, False)

# Enable specific features
enabled_features["auth"] = True
enabled_features["caching"] = True

print(enabled_features)
# {'auth': True, 'logging': False, 'caching': True, 
#  'compression': False, 'encryption': False}

Using with set operations

# Get unique elements and create a lookup
data = [1, 2, 3, 2, 1, 4, 3, 5]

unique_values = set(data)
lookup = dict.fromkeys(unique_values, 0)

print(lookup)
# {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}

fromkeys() vs Dictionary Comprehension

Aspectdict.fromkeys()Dict Comprehension
Syntaxdict.fromkeys(keys, value){k: value for k in keys}
Mutable defaultShares same object!Each key gets separate object
ReadabilityCleaner for simple casesMore explicit
FlexibilityLimited to same valueCan use different values per key

When to use each

Use .fromkeys() when:

  • All values should be the same
  • Values are immutable (numbers, strings, tuples)
  • You want cleaner syntax for simple initialization

Use dict comprehension when:

  • Values depend on the key
  • You need unique mutable objects per key
  • You need conditional logic for values

See Also

  • dict() — the dict constructor for creating new dictionaries
  • dict.get() — safely retrieve values from a dictionary with defaults
  • collections module — contains defaultdict for automatic default value creation