{"id":24064,"date":"2021-10-14T09:12:37","date_gmt":"2021-10-14T03:42:37","guid":{"rendered":"https:\/\/python-programs.com\/?p=24064"},"modified":"2021-11-05T17:13:36","modified_gmt":"2021-11-05T11:43:36","slug":"python-program-for-dictionary-setdefault-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-dictionary-setdefault-method\/","title":{"rendered":"Python Program for Dictionary setdefault() Method"},"content":{"rendered":"
In the previous article, we have discussed Python Program for floor() Function<\/a> 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 Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dictionary setdefault() Function:<\/strong><\/p>\n The Python Dictionary setdefault() function is one of several Dictionary functions that can be used to print the value at a specific key position. It prints None if there is no value at the given index.<\/p>\n Syntax:<\/strong><\/p>\n Parameter values:<\/strong><\/p>\n Keyname: <\/strong>This is required.The keyname of the item from which you want to retrieve the value.<\/p>\n Value:<\/strong> This is optional. This parameter has no effect if the key exists. If the key does not exist, this value takes its place. The default value is None.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Learn several Python Dictionary Method Examples<\/a> on our page and access the dictionary items easily.<\/p>\n In the previous article, we have discussed Python Program for floor() Function 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 dictionary = {‘hello’: 25, ‘this’: 35, ‘is’: 45, ‘btechgeeks’: 55} Output: The above …<\/p>\n
\nDictionary in python :<\/strong><\/p>\nGiven dictionary = {'hello': 25, 'this': 35, 'is': 45, 'btechgeeks': 55}<\/pre>\n
The above given dictionary is :\r\n{'hello': 25, 'this': 35, 'is': 45, 'btechgeeks': 55}\r\nThe value at the given key hello = 25\r\nThe value at the given key btechgeeks= 55\r\nThe value at the given key good = None\r\nThe value at the given key morning = None<\/pre>\n
Given dictionary = {100: 'good', 150: 'morning', 200: 'btechgeeks'}<\/pre>\n
The above given dictionary is :\r\n{100: 'good', 150: 'morning', 200: 'btechgeeks'}\r\nThe value at the given key 200 = btechgeeks\r\nThe value at the given key 10 = None\r\nThe value at the given key 150 = morning<\/pre>\n
Program for Dictionary setdefault() Method in Python<\/h2>\n
\n
dictionary.setdefault(keyname, value)<\/pre>\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the dictionary by initializing it with some random values and store it\r\n# in a variable.\r\ngvn_dictinry = {'hello': 25, 'this': 35, 'is': 45, 'btechgeeks': 55}\r\n# Print the above-given dictionary.\r\nprint(\"The above given dictionary is :\")\r\nprint(gvn_dictinry)\r\n# Apply dictionary.setdefault() method to the above-given dictionary to print the\r\n# value at a specific key position and print it.\r\n# It prints None if there is no value at the given index.\r\nprint(\"The value at the given key hello = \",\r\n gvn_dictinry.setdefault('hello', None))\r\n# similarly do the same for the other keys and print them.\r\nprint(\"The value at the given key btechgeeks= \",\r\n gvn_dictinry.setdefault('btechgeeks', None))\r\nprint(\"The value at the given key good = \",\r\n gvn_dictinry.setdefault('good', None))\r\nprint(\"The value at the given key morning = \",\r\n gvn_dictinry.setdefault('morning', None))\r\n<\/pre>\n
The above given dictionary is :\r\n{'hello': 25, 'this': 35, 'is': 45, 'btechgeeks': 55}\r\nThe value at the given key hello = 25\r\nThe value at the given key btechgeeks= 55\r\nThe value at the given key good = None\r\nThe value at the given key morning = None<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Take a dictionary and initialize it with an empty dictionary using dict() or {}.\r\ngvn_dictinry = {}\r\n# Give the number of keys as user input using int(input()) and store it in a variable.\r\nnumb_of_kys = int(\r\n input('Enter some random number of keys of the dictionary = '))\r\n# Loop till the given number of keys using for loop.\r\nfor p in range(numb_of_kys):\r\n # Inside the for loop scan the key and value as\r\n # user input using input(),split() functions\r\n # and store them in two separate variables.\r\n keyy, valuee = input(\r\n 'Enter key and value separated by spaces = ').split()\r\n # Initialize the key with the value of the dictionary.\r\n gvn_dictinry[keyy] = valuee\r\n\r\n# Print the above-given dictionary.\r\nprint(\"The above given dictionary is :\")\r\nprint(gvn_dictinry)\r\n# Give the key that you want to fetch the value as user input using the input()\r\n# function and store it in another variable.\r\ngvn_key1 = input(\"Enter some random number = \")\r\n# Apply dictionary.setdefault() method to the above-given dictionary to print the\r\n# value at a given specific key position and print it.\r\n# It prints None if there is no value at the given index.\r\nprint(\"The value at the given key\", gvn_key1, \" = \",\r\n gvn_dictinry.setdefault(gvn_key1, None))\r\n# similarly do the same for the other keys and print them.\r\ngvn_key2 = input(\"Enter some random number = \")\r\nprint(\"The value at the given key\", gvn_key2, \" = \",\r\n gvn_dictinry.setdefault(gvn_key2, None))\r\n<\/pre>\n
Enter some random number of keys of the dictionary = 3\r\nEnter key and value separated by spaces = 20 good\r\nEnter key and value separated by spaces = 40 morning\r\nEnter key and value separated by spaces = 60 btechgeeks\r\nThe above given dictionary is :\r\n{'20': 'good', '40': 'morning', '60': 'btechgeeks'}\r\nEnter some random number = 40\r\nThe value at the given key 40 = morning\r\nEnter some random number = 100\r\nThe value at the given key 100 = None<\/pre>\n
\n