{"id":23847,"date":"2021-10-05T20:28:54","date_gmt":"2021-10-05T14:58:54","guid":{"rendered":"https:\/\/python-programs.com\/?p=23847"},"modified":"2021-11-22T18:33:25","modified_gmt":"2021-11-22T13:03:25","slug":"python-program-for-dictionary-fromkeys-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-dictionary-fromkeys-method\/","title":{"rendered":"Python Program for Dictionary fromkeys() Method"},"content":{"rendered":"
Dictionary in python :<\/strong><\/p>\n A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element\u2019s keys.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n fromkeys() Method:<\/strong><\/p>\n The dict.fromkeys() method returns a new dictionary with the specified value and the given iterable (string, list, set, tuple) as keys.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n sequence:<\/strong><\/p>\n This is required. A sequence\/iterable, the elements of which will be used as keys in the new dictionary. This is required. Each key’s value. None is the default value.<\/p>\n Return Value:<\/strong><\/p>\n This function returns a new dictionary.<\/p>\n Using the dict.fromkeys() method, the following creates a new dict object.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n II) without Value:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n II) without Value:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dictionary in python : A dictionary is a set of elements that have key-value pairs. The values in the elements are accessed using the element\u2019s 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’: …<\/p>\nGiven keys = {'hello', 'this', 'is', 'btechgeeks'}\r\nGiven value = 'python'<\/pre>\n
The dictonary obtained for the given keys and values is :\r\n{'btechgeeks': 'python', 'hello': 'python', 'is': 'python', 'this': 'python'}<\/pre>\n
Here the value is given as 'python', so it assigns 'python' to all the keys in a obtained dictionary.<\/pre>\n
Given keys = {'hello', 'this', 'is', 'btechgeeks'}<\/pre>\n
The dictonary obtained for the given keys without value given is :\r\n{'btechgeeks': None, 'this': None, 'hello': None, 'is': None}<\/pre>\n
Here there is no value given given, hence it assigns 'none' as a value to all the keys in a obtained dictionary.<\/pre>\n
Program for Dictionary fromkeys() Method in Python<\/h2>\n
\n
dictionary.fromkeys(sequence, value)<\/code><\/pre>\n
\nvalue:<\/strong><\/p>\nMethod #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the keys as set(static input) and store it in a variable.\r\ngvn_keyss = {'hello', 'this', 'is', 'btechgeeks'}\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = 'python'\r\n# Apply the fromkeys method for the given keys and values which\r\n# returns the dictionary.\r\n# Store it in another variable.\r\ndictionary = dict.fromkeys(gvn_keyss, gvn_valu)\r\nprint(\"The dictonary obtained for the given keys and values is :\")\r\n# Print the above dictionary for the given keys and values.\r\nprint(dictionary)\r\n<\/pre>\n
The dictonary obtained for the given keys and values is :\r\n{'is': 'python', 'this': 'python', 'hello': 'python', 'btechgeeks': 'python'}<\/pre>\n
Here the value is given as 'python', so it assigns 'python' to all the keys in a obtained dictionary.<\/pre>\n
\n
# Give the keys as set(static input) and store it in a variable.\r\ngvn_keyss = {'hello', 'this', 'is', 'btechgeeks'}\r\n# Apply the fromkeys() method for the given keys which returns the dictionary.\r\n# Store it in another variable.\r\ndictionary = dict.fromkeys(gvn_keyss)\r\nprint(\"The dictonary obtained for the given keys without value given is :\")\r\n# Print the above dictionary for the given keys.\r\nprint(dictionary)\r\n<\/pre>\n
The dictonary obtained for the given keys without value given is :\r\n{'btechgeeks': None, 'this': None, 'hello': None, 'is': None}<\/pre>\n
Here there is no value given given, hence it assigns 'none' as a value to all the keys in a obtained dictionary.<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the keys as set(static input) and store it in a variable.\r\ngvn_keyss = set(input(\"Enter some random keys separated by spaces = \").split())\r\n# Give the value as user input using the input() function and store it in another variable.\r\ngvn_valu = input(\"Enter some random String = \")\r\n# Apply the fromkeys method for the given keys and values which\r\n# returns the dictionary.\r\n# Store it in another variable.\r\ndictionary = dict.fromkeys(gvn_keyss, gvn_valu)\r\nprint(\"The dictonary obtained for the given keys and values is :\")\r\n# Print the above dictionary for the given keys and values.\r\nprint(dictionary)\r\n<\/pre>\n
Enter some random keys separated by spaces = hello this is btechgeeks\r\nEnter some random String = python\r\nThe dictonary obtained for the given keys and values is :\r\n{'btechgeeks': 'python', 'hello': 'python', 'is': 'python', 'this': 'python'}<\/pre>\n
\n
# Give the keys as set(static input) and store it in a variable.\r\ngvn_keyss = set(input(\"Enter some random keys separated by spaces = \").split())\r\n# Apply the fromkeys() method for the given keys which returns the dictionary.\r\n# Store it in another variable.\r\ndictionary = dict.fromkeys(gvn_keyss)\r\nprint(\"The dictonary obtained for the given keys without value given is :\")\r\n# Print the above dictionary for the given keys.\r\nprint(dictionary)\r\n<\/pre>\n
Enter some random keys separated by spaces = hello this is btechgeeks\r\nThe dictonary obtained for the given keys without value given is :\r\n{'hello': None, 'btechgeeks': None, 'this': None, 'is': None}<\/pre>\n","protected":false},"excerpt":{"rendered":"