Python Dictionary fromkeys()

The fromkeys() method creates a dictionary from the given sequence of keys and values.

Example

# keys for the dictionary
alphabets = {'a', 'b', 'c'}

# value for the dictionary
number = 1

# creates a dictionary with keys and values dictionary = dict.fromkeys(alphabets, number)
print(dictionary) # Output: {'a': 1, 'c': 1, 'b': 1}

fromkeys() Syntax

The syntax of the fromkeys() method is:

dict.fromkeys(alphabets,number)

Here, alphabets and numbers are the key and value of the dictionary.


fromkeys() Parameters

The fromkeys() method can take two parameters:

  • alphabets - are the keys that can be any iterables like string, set, list, etc.
  • numbers (Optional) - are the values that can be of any type or any iterables like string, set, list, etc.

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


fromkeys() Return Value

The fromkeys() method returns:

  • a new dictionary with the given sequence of keys and values

Note: If the value of the dictionary is not provided, None is assigned to the keys.


Example 1: Python Dictionary fromkeys() with Key and Value

# set of vowels
keys = {'a', 'e', 'i', 'o', 'u' }

# assign string to the value
value = 'vowel'

# creates a dictionary with keys and values vowels = dict.fromkeys(keys, value)
print(vowels)

Output

{'a': 'vowel', 'u': 'vowel', 'e': 'vowel', 'i': 'vowel', 'o': 'vowel'}

In the above example, we have used the fromkeys() method to create the dictionary with the given set of keys and string value.

Here, the fromkeys() method creates a new dictionary named vowels from keys and value. All the keys of the dictionary are assigned with the same value.


Example 2: fromkeys() without Value

# list of numbers
keys = [1, 2, 4 ]

# creates a dictionary with keys only numbers = dict.fromkeys(keys)
print(numbers)

Output

{1: None, 2: None, 4: None}

In the above example, we have created a dictionary named numbers with the given list of keys.

We have not provided any values, so all the keys are assigned None by default.


Example 3: fromkeys() To Create A Dictionary From Mutable Object

# set of vowels
keys = {'a', 'e', 'i', 'o', 'u' }

# list of number
value = [1]

vowels = dict.fromkeys(keys, value)
print(vowels)

# updates the list value
value.append(2)

print(vowels)

Output

{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]}
{'a': [1, 2], 'u': [1, 2], 'o': [1, 2], 'e': [1, 2], 'i': [1, 2]}

In the above example, we have used a list as the value for the dictionary. The iterables like list, dictionary, etc are the mutable objects, meaning they can be modified.

Here, when we update the list value using append, the keys are assigned with the new updated values. This is because each element is pointing to the same address in the memory.

To solve this problem, we can use dictionary comprehension.


Dictionary comprehension for mutable objects

We can use dictionary comprehension and prevent updating the dictionary when the mutable object (list, dictionary, etc) is updated. For example,

# vowels keys
keys = {'a', 'e', 'i', 'o', 'u' }
value = [1]

# creates dictionary using dictionary comprehension vowels = { key : list(value) for key in keys }
print(vowels) # updates the value list value.append(2) print(vowels)

Output

{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]}
{'a': [1], 'u': [1], 'o': [1], 'e': [1], 'i': [1]}

In the above example, we have used dictionary comprehension to create a dictionary named vowels.

Here, the value is not assigned to the keys of the dictionary. But, for each key in keys, a new list of value is created. The newly created list is assigned each key in the dictionary.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges