{"id":5516,"date":"2023-10-27T11:53:54","date_gmt":"2023-10-27T06:23:54","guid":{"rendered":"https:\/\/python-programs.com\/?p=5516"},"modified":"2023-11-10T12:03:11","modified_gmt":"2023-11-10T06:33:11","slug":"python-pretty-print-nested-dictionaries-dict-of-dicts","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-pretty-print-nested-dictionaries-dict-of-dicts\/","title":{"rendered":"Python: Pretty Print Nested Dictionaries \u2013 Dict of Dicts"},"content":{"rendered":"
Dictionaries are Python\u2019s implementation of an associative list, which\u00a0may be a\u00a0<\/span>arrangement\u00a0<\/span>. A dictionary\u00a0may be a\u00a0<\/span>collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair.<\/p>\n To an infinite depth, a dictionary may include another dictionary, which can contain dictionaries, and so on. This is referred to as a nested dictionary.<\/p>\n Nested dictionaries are\u00a0one among\u00a0<\/span>several ways\u00a0during which\u00a0<\/span>structured information is represented (similar to records or structures in other languages).<\/p>\n In this article, we’ll look at how to print a nested dictionary in a visually beautiful and readable format.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to print nested dictionaries in pretty way some of them are:<\/p>\n We can only call the values() function of a dictionary for a normal dictionary to get an iterable sequence of values. However, a value can be another dictionary object in a nested dictionary. To do this, we need to call the values() function again and get a new iterative sequence of values and search for dictation objects in those values. This is possible with recurrence in a simple way.<\/p>\n To print all of the key-values in this dictionary, we wrote a function that will recursively go through the nested dictionary and print all of the key-values.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Rather than writing our own functions, we can use the json module to print a dictionary of dictionaries in a format that looks like json.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can print the nested dictionary a table using the pandas module.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n Dictionaries are Python\u2019s implementation of an associative list, which\u00a0may be a\u00a0arrangement\u00a0. A dictionary\u00a0may be a\u00a0collection of key-value pairs that are stored together. A key and its value are represented by each key-value pair. To an infinite depth, a dictionary may include another dictionary, which can contain dictionaries, and so on. This is referred to as …<\/p>\nnesteddict = {\r\n'hello': {'www': 100, 'yyy': 'Helloworld'},\r\n'this': {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},\r\n'BTechGeeks': {'PLACE': 'HYDERABAD', 'PINCODE': 500000,\r\n'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},\r\n}<\/pre>\n
{\r\n \"hello\": {\r\n \"www\": 100,\r\n \"yyy\": \"Helloworld\"\r\n },\r\n \"this\": {\r\n \"www\": \"vikram\",\r\n \"age\": 20,\r\n \"DOB\": \"FEB\"\r\n },\r\n \"BTechGeeks\": {\r\n \"PLACE\": \"HYDERABAD\",\r\n \"PINCODE\": 500000,\r\n \"PYTHON\": {\r\n \"FUNCTIONS\": \"Built in\",\r\n \"year\": 1999\r\n }\r\n }\r\n}<\/pre>\n
Print nested dictionaries in pretty way<\/h2>\n
\n
1)Using generic function<\/h3>\n
def printNested(nesteddict):\r\n # Traverse the dictionary\r\n for key, value in nesteddict.items():\r\n # If the value is of the dictionary type, then print\r\n # all of the key and values within the nested dictionary.\r\n if isinstance(value, dict):\r\n print(key, \" : {\\n\")\r\n printNested(value)\r\n print(\"\\n}\\n\")\r\n\r\n else:\r\n print(\"\\t\", key, \" : \", value)\r\n\r\n\r\n# given nested_dictionary\r\nnesteddict = {\r\n 'hello': {'www': 100, 'yyy': 'Helloworld'},\r\n 'this': {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},\r\n 'BTechGeeks': {'PLACE': 'HYDERABAD', 'PINCODE': 500000,\r\n 'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},\r\n}\r\n# passing nested dictionary to printNested Function\r\nprintNested(nesteddict)\r\n<\/pre>\n
hello : {\r\n\r\n\t www : 100\r\n\t yyy : Helloworld\r\n\r\n}\r\n\r\nthis : {\r\n\r\n\t www : vikram\r\n\t age : 20\r\n\t DOB : FEB\r\n\r\n}\r\n\r\nBTechGeeks : {\r\n\r\n\t PLACE : HYDERABAD\r\n\t PINCODE : 500000\r\nPYTHON : {\r\n\r\n\t FUNCTIONS : Built in\r\n\t year : 1999\r\n\r\n}\r\n}<\/pre>\n
2)Using json module<\/h3>\n
import json as json\r\n# given nested_dictionary\r\nnesteddict = {\r\n 'hello': {'www': 100, 'yyy': 'Helloworld'},\r\n 'this': {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},\r\n 'BTechGeeks': {'PLACE': 'HYDERABAD', 'PINCODE': 500000,\r\n 'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},\r\n}\r\n# using json to print the nested dict\r\nprint(json.dumps(nesteddict, indent=4))\r\n<\/pre>\n
{\r\n \"hello\": {\r\n \"www\": 100,\r\n \"yyy\": \"Helloworld\"\r\n },\r\n \"this\": {\r\n \"www\": \"vikram\",\r\n \"age\": 20,\r\n \"DOB\": \"FEB\"\r\n },\r\n \"BTechGeeks\": {\r\n \"PLACE\": \"HYDERABAD\",\r\n \"PINCODE\": 500000,\r\n \"PYTHON\": {\r\n \"FUNCTIONS\": \"Built in\",\r\n \"year\": 1999\r\n }\r\n }\r\n}<\/pre>\n
3)Using pandas to print as table<\/h3>\n
import pandas as pd\r\n# given nested_dictionary\r\nnesteddict = {\r\n 'hello': {'www': 100, 'yyy': 'Helloworld'},\r\n 'this': {'www': 'vikram', 'age': 20, 'DOB': 'FEB'},\r\n 'BTechGeeks': {'PLACE': 'HYDERABAD', 'PINCODE': 500000,\r\n 'PYTHON': {'FUNCTIONS': 'Built in', 'year': 1999}},\r\n}\r\nprint(pd.DataFrame(nesteddict).T)\r\n<\/pre>\n
DOB PINCODE PLACE ... age www yyy\r\nhello NaN NaN NaN ... NaN 100 Helloworld\r\nthis FEB NaN NaN ... 20 vikram NaN\r\nBTechGeeks NaN 500000 HYDERABAD ... NaN NaN NaN\r\n\r\n[3 rows x 7 columns]<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n