Python dict fromkeys – Python Program for Dictionary fromkeys() Method

Python dict fromkeys: In the previous article, we have discussed Python Program for Dictionary clear() Function
Dictionary in python :

dict.fromkeys: A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element’s keys.

Examples:

Example1:

Input:

Given keys = {'hello', 'this', 'is', 'btechgeeks'}
Given value = 'python'

Output:

The dictonary obtained for the given keys and values is :
{'btechgeeks': 'python', 'hello': 'python', 'is': 'python', 'this': 'python'}

Explanation:

Here the value is given as 'python', so it assigns 'python' to all the keys in a obtained dictionary.

Example2:

Input:

Given keys = {'hello', 'this', 'is', 'btechgeeks'}

Output:

The dictonary obtained for the given keys without value given is :
{'btechgeeks': None, 'this': None, 'hello': None, 'is': None}

Explanation:

Here there is no value given given, hence it assigns 'none' as a value to all the keys in a obtained dictionary.

Program for Dictionary fromkeys() Method in Python

fromkeys() Method:

Python dict fromkeys: The dict.fromkeys() method returns a new dictionary with the specified value and the given iterable (string, list, set, tuple) as keys.

Syntax:

dictionary.fromkeys(sequence, value)

Parameters:

sequence:

This is required. A sequence/iterable, the elements of which will be used as keys in the new dictionary.
value:

This is required. Each key’s value. None is the default value.

Return Value:

This function returns a new dictionary.

Using the dict.fromkeys() method, the following creates a new dict object.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the keys as a set(static input) and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Apply the fromkeys() method for the given keys and values which return the dictionary.
  • Store it in another variable.
  • Print the above dictionary for the given keys and values.
  • The Exit of Program.

Below is the implementation:

# Give the keys as set(static input) and store it in a variable.
gvn_keyss = {'hello', 'this', 'is', 'btechgeeks'}
# Give the value as static input and store it in another variable.
gvn_valu = 'python'
# Apply the fromkeys method for the given keys and values which
# returns the dictionary.
# Store it in another variable.
dictionary = dict.fromkeys(gvn_keyss, gvn_valu)
print("The dictonary obtained for the given keys and values is :")
# Print the above dictionary for the given keys and values.
print(dictionary)

Output:

The dictonary obtained for the given keys and values is :
{'is': 'python', 'this': 'python', 'hello': 'python', 'btechgeeks': 'python'}

Explanation:

Here the value is given as 'python', so it assigns 'python' to all the keys in a obtained dictionary.

II) without Value:

Approach:

  • Give the keys as a set(static input) and store it in a variable.
  • Apply the fromkeys() method for the given keys which returns the dictionary.
  • Store it in another variable.
  • Print the above dictionary for the given keys.
  • The Exit of Program.

Below is the implementation:

# Give the keys as set(static input) and store it in a variable.
gvn_keyss = {'hello', 'this', 'is', 'btechgeeks'}
# Apply the fromkeys() method for the given keys  which returns the dictionary.
# Store it in another variable.
dictionary = dict.fromkeys(gvn_keyss)
print("The dictonary obtained for the given keys without value given is :")
# Print the above dictionary for the given keys.
print(dictionary)

Output:

The dictonary obtained for the given keys without value given is :
{'btechgeeks': None, 'this': None, 'hello': None, 'is': None}

Explanation:

Here there is no value given given, hence it assigns 'none' as a value to all the keys in a obtained dictionary.

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the keys as a set(user input) using set(), input(), and split() functions and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Apply the fromkeys() method for the given keys and values which return the dictionary.
  • Store it in another variable.
  • Print the above dictionary for the given keys and values.
  • The Exit of Program.

Below is the implementation:

# Give the keys as set(static input) and store it in a variable.
gvn_keyss = set(input("Enter some random keys separated by spaces = ").split())
# Give the value as user input using the input() function and store it in another variable.
gvn_valu = input("Enter some random String = ")
# Apply the fromkeys method for the given keys and values which
# returns the dictionary.
# Store it in another variable.
dictionary = dict.fromkeys(gvn_keyss, gvn_valu)
print("The dictonary obtained for the given keys and values is :")
# Print the above dictionary for the given keys and values.
print(dictionary)

Output:

Enter some random keys separated by spaces = hello this is btechgeeks
Enter some random String = python
The dictonary obtained for the given keys and values is :
{'btechgeeks': 'python', 'hello': 'python', 'is': 'python', 'this': 'python'}

II) without Value:

Approach:

  • Give the keys as a set(user input) using set(), input(), and split() functions and store it in a variable.
  • Apply the fromkeys() method for the given keys which returns the dictionary.
  • Store it in another variable.
  • Print the above dictionary for the given keys.
  • The Exit of Program.

Below is the implementation:

# Give the keys as set(static input) and store it in a variable.
gvn_keyss = set(input("Enter some random keys separated by spaces = ").split())
# Apply the fromkeys() method for the given keys which returns the dictionary.
# Store it in another variable.
dictionary = dict.fromkeys(gvn_keyss)
print("The dictonary obtained for the given keys without value given is :")
# Print the above dictionary for the given keys.
print(dictionary)

Output:

Enter some random keys separated by spaces = hello this is btechgeeks
The dictonary obtained for the given keys without value given is :
{'hello': None, 'btechgeeks': None, 'this': None, 'is': None}