Python program to convert XML to Dictionary Last Updated : 05 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss how to convert an XML to a dictionary using Python.Modules Usedxmltodict: It is a Python module that makes working with XML feel like you are working with [JSON]. Run the following command in the terminal to install the module.Syntax:pip install xmltodictpprint: The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a well-formatted and more readable way.ApproachImport necessary module to working space.Open the XML File in Read-only mode and read the contents using file.read() and store it in a variable.Syntax:with open('filename', 'r', encoding='utf-8') as file: my_xml = file.read()Use xmltodict.parse() to parse the content from the variable and convert it into Dictionary.Syntax : xmltodict.parse(xml_input, encoding='utf-8', expat=expat, process_namespaces=False, namespace_separator=':', **kwargs) Use pprint(pretty print) to print the dictionary in well-formatted and readable way.Syntax : pprint.pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True)Example: Converting XML to dictionaryFile Used: Python # Import the required modules import xmltodict import pprint # Open the file and read the contents with open('example.xml', 'r', encoding='utf-8') as file: my_xml = file.read() # Use xmltodict to parse and convert # the XML document my_dict = xmltodict.parse(my_xml) # Print the dictionary pprint.pprint(my_dict, indent=2) Output:Example 2: Converting XML to dictionaryFile Used: Python # Import the required modules import xmltodict import pprint # Open the file and read the contents with open('example_2.xml', 'r', encoding='utf-8') as file: my_xml = file.read() # Use xmltodict to parse and convert the # XML document my_dict = xmltodict.parse(my_xml) # Print the dictionary pprint.pprint(my_dict, indent=2) Output: Comment More infoAdvertise with us Next Article Python - Convert Nested dictionary to Mapped Tuple A anilabhadatta Follow Improve Article Tags : Python Python Programs Blogathon Blogathon-2021 Python-XML +1 More Practice Tags : python Similar Reads Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2 3 min read Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2 3 min read Python Convert Dictionary to List of Values Python has different types of built-in data structures to manage your data. A list is a collection of ordered items, whereas a dictionary is a key-value pair data. Both of them are unique in their own way. In this article, the dictionary is converted into a list of values in Python using various con 3 min read Python | Dictionary to list of tuple conversion Inter conversion between the datatypes is a problem that has many use cases and is usual subproblem in the bigger problem to solve. The conversion of tuple to dictionary has been discussed before. This article discusses a converse case in which one converts the dictionary to list of tuples as the wa 5 min read Python - Convert Nested dictionary to Mapped Tuple The task is to convert a nested dictionary into a mapping where each key's values are transformed into tuples. This involves taking the inner dictionaries and creating tuples for each key-value pair across the nested structure. If a key appears in multiple inner dictionaries, its values should be gr 4 min read Convert a Set into dictionary - Python The task is to convert a set into a dictionary in Python. Set is an unordered collection of unique elements, while a dictionary stores key-value pairs. When converting a set into a dictionary, each element in the set can be mapped to a key and a default value can be assigned to each key.For example, 3 min read Like