AttrDict is an MIT-licensed library which provides mapping objects that allow their elements to be accessed both as keys and as attributes.
So we can think of the dictionary that we import and use.
Installation:
To install AttrDict, use the pip command as follows:
pip install attrdict
Having installed it, let us understand it with the working of a program:
Example 1 :Here we will show how to create a dictionary pair using the module.
Python3
from attrdict import AttrDict
dictionary = AttrDict({"Geeks" : "forGeeks"})
print(dictionary.Geeks)
print(dictionary["Geeks"])
|
Output :
forGeeks
forGeeks
Example 2 :Making a Nested dictionary of multiple elements and printing them.
Python3
from attrdict import AttrDict
dictionary = AttrDict({'foo': 'bar',
'alpha': {'beta': 'a',
'a': 'a'}})
for key in dictionary:
print(dictionary[key])
|
Output :
bar
{'beta': 'a', 'a': 'a'}
Example 3 :Adding another dictionary to a dictionary.
Python3
from attrdict import AttrDict
a = {'foo': 'bar', 'alpha': {'beta': 'a', 'a': 'a'}}
b = {'lorem': 'ipsum', 'alpha': {'bravo': 'b', 'a': 'b'}}
c = AttrDict(a) + b
print(type(c))
print(c)
|
Output :
<class 'attrdict.dictionary.AttrDict'>
AttrDict({'foo': 'bar', 'lorem': 'ipsum', 'alpha': {'beta': 'a', 'bravo': 'b', 'a': 'b'}})
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!