{"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

Given keys = {'hello', 'this', 'is', 'btechgeeks'}\r\nGiven value = 'python'<\/pre>\n

Output:<\/strong><\/p>\n

The dictonary obtained for the given keys and values is :\r\n{'btechgeeks': 'python', 'hello': 'python', 'is': 'python', 'this': 'python'}<\/pre>\n

Explanation:<\/strong><\/p>\n

Here the value is given as 'python', so it assigns 'python' to all the keys in a obtained dictionary.<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given keys = {'hello', 'this', 'is', 'btechgeeks'}<\/pre>\n

Output:<\/strong><\/p>\n

The dictonary obtained for the given keys without value given is :\r\n{'btechgeeks': None, 'this': None, 'hello': None, 'is': None}<\/pre>\n

Explanation:<\/strong><\/p>\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