{"id":25492,"date":"2021-11-16T08:40:07","date_gmt":"2021-11-16T03:10:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=25492"},"modified":"2021-11-16T08:40:07","modified_gmt":"2021-11-16T03:10:07","slug":"python-dict-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-dict-function-with-examples\/","title":{"rendered":"Python dict() Function with Examples"},"content":{"rendered":"
dict() Function in Python:<\/strong><\/p>\n The dict() function is used to create a dictionary.<\/p>\n A dictionary is an unordered, changeable, and indexed collection.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n keyword arguments:<\/strong> This is Required. You can use as many keyword arguments as you want, separated by a comma: key = value, key = value…<\/p>\n Return Value:<\/strong><\/p>\n dict() does not return any results (returns None).<\/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 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 dict() Function in Python: The dict() function is used to create a dictionary. A dictionary is an unordered, changeable, and indexed collection. Syntax: dict(keyword arguments) Parameters keyword arguments: This is Required. You can use as many keyword arguments as you want, separated by a comma: key = value, key = value… Return Value: dict() does …<\/p>\ndict(keyword arguments)<\/pre>\n
Given key-value pairs = (emp_name=\"sunny\", emp_id=2122, jobrole=\"software developer\")<\/pre>\n
The result dictionary is :\r\n{'emp_name': 'sunny', 'emp_id': 2122, 'jobrole': 'software developer'}<\/pre>\n
Given key-value pairs = (one=\"hello\", two=\"good\", three=\"morning\")<\/pre>\n
The result dictionary is :\r\n{'one': 'hello', 'two': 'good', 'three': 'morning'}<\/pre>\n
dict() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Pass some random key-value pairs as the arguments to the dict() function\r\n# and store it in a variable.\r\nrslt = dict(emp_name=\"sunny\", emp_id=2122, jobrole=\"software developer\")\r\n# Print the above result\r\nprint(\"The result dictionary is :\")\r\nprint(rslt)\r\n<\/pre>\n
The result dictionary is :\r\n{'emp_name': 'sunny', 'emp_id': 2122, 'jobrole': 'software developer'}<\/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 = 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\nprint(\"The result dictionary is :\")\r\nprint(gvn_dict)<\/pre>\n
Enter some random number of keys of the dictionary = 3\r\nEnter key and value separated by spaces = one hello\r\nEnter key and value separated by spaces = two good\r\nEnter key and value separated by spaces = three morning\r\nThe result dictionary is :\r\n{'one': 'hello', 'two': 'good', 'three': 'morning'}<\/pre>\n","protected":false},"excerpt":{"rendered":"