{"id":23850,"date":"2021-10-05T20:28:49","date_gmt":"2021-10-05T14:58:49","guid":{"rendered":"https:\/\/python-programs.com\/?p=23850"},"modified":"2021-11-22T18:33:25","modified_gmt":"2021-11-22T13:03:25","slug":"python-program-for-dictionary-clear-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-dictionary-clear-function\/","title":{"rendered":"Python Program for Dictionary clear() Function"},"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 Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dictionary clear() Function:<\/strong><\/p>\n clear() is a Python dictionary method that removes all items from the dictionary.<\/p>\n Syntax:<\/strong><\/p>\n Parameters: <\/strong>NA<\/p>\n Return Value: <\/strong>This method has no return value.<\/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 <\/p>\n","protected":false},"excerpt":{"rendered":" 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’: 100, ‘this’: 200, ‘is’: 300, ‘btechgeeks’: 400} Output: The given dictionary before clearing is : {‘hello’: 100, ‘this’: 200, ‘is’: 300, ‘btechgeeks’: …<\/p>\nGiven dictionary = {'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}<\/pre>\n
The given dictionary before clearing is :\r\n{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}\r\nThe length of the given dictionary before clearing = 4\r\nThe given dictionary After clearing is :\r\n{}\r\nThe length of the given dictionary After clearing = 0<\/pre>\n
Given dictionary = {'good': '1', 'morning': '2', 'this': '3', 'is': '4', 'btechgeeks': '5'}<\/pre>\n
The given dictionary before clearing is :\r\n{'good': '1', 'morning': '2', 'this': '3', 'is': '4', 'btechgeeks': '5'}\r\nThe length of the given dictionary before clearing = 5\r\nThe given dictionary After clearing is :\r\n{}\r\nThe length of the given dictionary After clearing = 0<\/pre>\n
Program for Dictionary clear() Function in Python<\/h2>\n
\n
dict.clear()<\/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': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}\r\n# Print the above-given dictionary before clearing.\r\nprint(\"The given dictionary before clearing is :\")\r\n# Calculate the length of the\u00a0given dictionary using the len() function and print it.\r\nprint(gvn_dictinry)\r\nprint(\"The length of the given dictionary before clearing = \", len(gvn_dictinry))\r\n# Apply clear() function to the given dictionary.\r\ngvn_dictinry.clear()\r\n# Print the above-given dictionary after clearing it.\r\nprint(\"The given dictionary After clearing is :\")\r\nprint(gvn_dictinry)\r\n# Calculate the length of the above dictionary after clearing using the len() function\r\n# and print it.\r\nprint(\"The length of the given dictionary After clearing = \", len(gvn_dictinry))\r\n<\/pre>\n
The given dictionary before clearing is :\r\n{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}\r\nThe length of the given dictionary before clearing = 4\r\nThe given dictionary After clearing is :\r\n{}\r\nThe length of the given dictionary After clearing = 0<\/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_dict = {}\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_dict[keyy] = valuee\r\n\r\n# Print the above-given dictionary before clearing.\r\nprint(\"The given dictionary before clearing is :\")\r\n# Calculate the length of the\u00a0given dictionary using the len() function and print it.\r\nprint(gvn_dict)\r\nprint(\"The length of the given dictionary before clearing = \", len(gvn_dict))\r\n# Apply clear() function to the given dictionary.\r\ngvn_dict.clear()\r\n# Print the above-given dictionary after clearing it.\r\nprint(\"The given dictionary After clearing is :\")\r\nprint(gvn_dict)\r\n# Calculate the length of the above dictionary after clearing using the len() function\r\n# and print it.\r\nprint(\"The length of the given dictionary After clearing = \", len(gvn_dict))\r\n<\/pre>\n
Enter some random number of keys of the dictionary = 5\r\nEnter key and value separated by spaces = good 1\r\nEnter key and value separated by spaces = morning 2\r\nEnter key and value separated by spaces = this 3\r\nEnter key and value separated by spaces = is 4\r\nEnter key and value separated by spaces = btechgeeks 5\r\nThe given dictionary before clearing is :\r\n{'good': '1', 'morning': '2', 'this': '3', 'is': '4', 'btechgeeks': '5'}\r\nThe length of the given dictionary before clearing = 5\r\nThe given dictionary After clearing is :\r\n{}\r\nThe length of the given dictionary After clearing = 0<\/pre>\n