{"id":23855,"date":"2021-10-05T20:29:01","date_gmt":"2021-10-05T14:59:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=23855"},"modified":"2021-11-22T18:33:25","modified_gmt":"2021-11-22T13:03:25","slug":"python-program-for-dictionary-items-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-dictionary-items-function\/","title":{"rendered":"Python Program for Dictionary items() 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 items() Function:<\/strong><\/p>\n Python’s items Dictionary function returns a list of dictionary items that are currently available (total keys and values).<\/p>\n The dictionary items function returns the total number of key-value pairs in a given dictionary.<\/p>\n Syntax:<\/strong><\/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 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 above given dictionary is : {‘hello’: 100, ‘this’: 200, ‘is’: 300, ‘btechgeeks’: 400} …<\/p>\nGiven dictionary = {'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}<\/pre>\n
The above given dictionary is :\r\n{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}\r\nThe above given dictionary items are :\r\ndict_items([('hello', 100), ('this', 200), ('is', 300), ('btechgeeks', 400)])\r\nThe above given new dictionary is :\r\n{}\r\nThe above given New empty dictionary items are :\r\ndict_items([])<\/pre>\n
Given dictionary = {'good': '1', 'morning': '3', 'this': '5', 'is': '7', 'btechgeeks': '9'}<\/pre>\n
The above given dictionary is :\r\n{'good': '1', 'morning': '3', 'this': '5', 'is': '7', 'btechgeeks': '9'}\r\nThe above given dictionary items are :\r\ndict_items([('good', '1'), ('morning', '3'), ('this', '5'), ('is', '7'), ('btechgeeks', '9')])\r\nThe above given new dictionary is :\r\n{}\r\nThe above given New empty dictionary items are :\r\ndict_items([])<\/pre>\n
Program for Dictionary items() Function in Python<\/h2>\n
\n
dictionary_name.items()<\/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.\r\nprint(\"The above given dictionary is :\")\r\nprint(gvn_dictinry)\r\n# Apply dictionary.items() method to the above-given dictionary and print it.\r\nprint(\"The above given dictionary items are :\")\r\nprint(gvn_dictinry.items())\r\n# Take a new empty dictionary and\u00a0store it in another variable.\r\nnew_dict = {}\r\n# Print the above-initialized new empty dictionary.\r\nprint(\"The above given new dictionary is :\")\r\nprint(new_dict)\r\n# Apply dictionary.items() method to the above new empty dictionary and print it.\r\nprint(\"The above given New empty dictionary items are :\")\r\nprint(new_dict.items())\r\n<\/pre>\n
The above given dictionary is :\r\n{'hello': 100, 'this': 200, 'is': 300, 'btechgeeks': 400}\r\nThe above given dictionary items are :\r\ndict_items([('hello', 100), ('this', 200), ('is', 300), ('btechgeeks', 400)])\r\nThe above given new dictionary is :\r\n{}\r\nThe above given New empty dictionary items are :\r\ndict_items([])<\/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# Apply dictionary.items() method to the above-given dictionary and print it.\r\nprint(\"The above given dictionary items are :\")\r\nprint(gvn_dictinry.items())\r\n# Take a new empty dictionary and\u00a0store it in another variable.\r\nnew_dict = {}\r\n# Print the above-initialized new empty dictionary.\r\nprint(\"The above given new dictionary is :\")\r\nprint(new_dict)\r\n# Apply dictionary.items() method to the above new empty dictionary and print it.\r\nprint(\"The above given New empty dictionary items are :\")\r\nprint(new_dict.items())<\/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 3\r\nEnter key and value separated by spaces = this 5\r\nEnter key and value separated by spaces = is 7\r\nEnter key and value separated by spaces = btechgeeks 9\r\nThe above given dictionary is :\r\n{'good': '1', 'morning': '3', 'this': '5', 'is': '7', 'btechgeeks': '9'}\r\nThe above given dictionary items are :\r\ndict_items([('good', '1'), ('morning', '3'), ('this', '5'), ('is', '7'), ('btechgeeks', '9')])\r\nThe above given new dictionary is :\r\n{}\r\nThe above given New empty dictionary items are :\r\ndict_items([])<\/pre>\n","protected":false},"excerpt":{"rendered":"