Get a dictionary from an Objects Fields Last Updated : 28 Jul, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will discuss how to get a dictionary from object's field i.e. how to get the class members in the form of a dictionary. There are two approaches to solve the above problem: By using the __dict__ attribute on an object of a class and attaining the dictionary. All objects in Python have an attribute __dict__, which is a dictionary object containing all attributes defined for that object itself. The mapping of attributes with its values is done to generate a dictionary.By calling the in-built vars method, which is used to return __dict__ attribute of a module, class, class instance, or an object. #Method 1: To generate a dictionary from an arbitrary object using __dict__attribute: Python3 # class Animals is declared class Animals: # constructor def __init__(self): # keys are initialized with # their respective values self.lion = 'carnivore' self.dog = 'omnivore' self.giraffe = 'herbivore' def printit(self): print("Dictionary from the object fields\ belonging to the class Animals:") # object animal of class Animals animal = Animals() # calling printit method animal.printit() # calling attribute __dict__ on animal # object and printing it print(animal.__dict__) Output: Dictionary from the object fields belonging to the class Animals: {'lion': 'carnivore', 'dog': 'omnivore', 'giraffe': 'herbivore'} #Method 2: To generate a dictionary from an arbitrary object using an in-built vars method: Python3 # class A is declared class A: # constructor def __init__(self): # keys are initialized with # their respective values self.A = 1 self.B = 2 self.C = 3 self.D = 4 # object obj of class A obj = A() # calling vars method on obj object print(vars(obj)) Output: {'A': 1, 'B': 2, 'C': 3, 'D': 4} Comment More infoAdvertise with us Next Article Iterate over a dictionary in Python M manandeep1610 Follow Improve Article Tags : Python python-dict Practice Tags : pythonpython-dict Similar Reads How to read Dictionary from File in Python? In Python, reading a dictionary from a file involves retrieving stored data and converting it back into a dictionary format. Depending on how the dictionary was savedâwhether as text, JSON, or binary-different methods can be used to read and reconstruct the dictionary for further use in your program 3 min read Python Dictionary items() method items() method in Python returns a view object that contains all the key-value pairs in a dictionary as tuples. This view object updates dynamically if the dictionary is modified.Example:Pythond = {'A': 'Python', 'B': 'Java', 'C': 'C++'} # using items() to get all key-value pairs items = d.items() p 2 min read Iterate over a dictionary in Python In this article, we will cover How to Iterate Through a Dictionary in Python. To Loop through values in a dictionary you can use built-in methods like values(), items() or even directly iterate over the dictionary to access values with keys.How to Loop Through a Dictionary in PythonThere are multipl 6 min read Convert nested Python dictionary to object Let us see how to convert a given nested dictionary into an object Method 1 : Using the json module. We can solve this particular problem by importing the json module and use a custom object hook in the json.loads() method. python3 # importing the module import json # declaringa a class class obj: # 2 min read Dictionary Methods ( Part 1 ) 1. str(dic) :- This method is used to return the string, denoting all the dictionary keys with their values. 2. items() :- This method is used to return the list with all dictionary keys with values. Python # Python code to demonstrate working of # str() and items() # Initializing dictionary dic = { 2 min read Python Dictionary keys() method keys() method in Python dictionary returns a view object that displays a list of all the keys in the dictionary. This view is dynamic, meaning it reflects any changes made to the dictionary (like adding or removing keys) after calling the method. Example:Pythond = {'A': 'Geeks', 'B': 'For', 'C': 'Ge 2 min read Like