Skip to main content

Python Dictionary fromkeys() Function

The Dictionary fromkeys() method creates a new dictionary from a given sequence of keys, with all keys set to a specified value.

This method is particularly useful when you need to initialize a dictionary with a set of keys and a default value for each key.

Syntax

my_dictionary.fromkeys(keys, value)

fromkeys() Parameters

Python Dictionary fromkeys() method parameters:

ParameterConditionDescription
keysRequiredAn iterable of keys for the new dictionary (any iterables like string, set, list, etc.)
valueOptionalThe value for all keys. Default value is None.
note

Note that the same value is assigned to all the keys of the dictionary.

fromkeys() Return Value

Python Dictionary fromkeys() function returns a new dictionary with keys mapped to the specified value.

note

Note that if the value of the dictionary is not provided, None is assigned to the keys.

Examples

Example 1: fromkeys() with Key and without Default Value

If default value argument is not specified, all keys are set to value None.

keys = ('a', 'b', 'c')
new_dict = dict.fromkeys(keys, None)
print(new_dict) # Output: {'a': None, 'b': None, 'c': None}

output

{'a': None, 'b': None, 'c': None}

Example 2: fromkeys() with Key and Default Value

For example, the new dictionary has keys a, b, and c all set to default value 0.

keys = ('a', 'b', 'c')
new_dict = dict.fromkeys(keys, 0)
print(new_dict) # Output: {'a': 0, 'b': 0, 'c': 0}

output

{'a': 0, 'b': 0, 'c': 0}

Example 3: Using fromkeys() with Mutable Objects as Values

This example demonstrates the behavior of fromkeys() when using mutable objects as values.

Since all keys are mapped to the same list object, changes to the list affect all values in the dictionary.

To avoid this behavior, you can use a dictionary comprehension to create a new list for each key.

keys = ('a', 'b', 'c')
value = [0]
new_dict = dict.fromkeys(keys, value)
print(new_dict) # Output: {'a': [0], 'b': [0], 'c': [0]}

# Appending to the list
value.append(1)
print(new_dict) # Output: {'a': [0, 1], 'b': [0, 1], 'c': [0, 1]}

output

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

Example 4: Preventing Sharing of Mutable Objects

This example uses a dictionary comprehension to create a new list for each key, preventing the issue of shared mutable objects found in the previous example

keys = ('a', 'b', 'c')
new_dict = {key: [0] for key in keys}
print(new_dict) # Output: {'a': [0], 'b': [0], 'c': [0]}

# Appending to the list
new_dict['a'].append(1)
print(new_dict) # Output: {'a': [0, 1], 'b': [0], 'c': [0]}

output

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

Equivalent Method: Dictionary Comprehension

An alternative way to initialize a dictionary from keys lists is using dictionary comprehension.

Dictionary Comprehension with Key List and a Default Value

my_list = ['Tom', 'David', 'Anna']
my_dict = {key:'Developer' for key in my_list}
print(my_dict) # Output: {'Bob': 'Developer', 'Sam': 'Developer'}

output

{'Tom': 'Developer', 'David': 'Developer', 'Anna': 'Developer'}

Dictionary Comprehension with Key List and without Default Value

my_list = ['Tom', 'David', 'Anna']
my_dict = {key:None for key in my_list}
print(my_dict) # Output: {'Tom': None, 'David': None, 'Anna': None}

output

{'Tom': None, 'David': None, 'Anna': None}