Back to all posts

EasyDict dictionaries

EasyDict is a subclass of Python's built-in dictionary that allows accessing dictionary keys as attributes like JavaScript object. pip install easydict fro…

EasyDict is a subclass of Python's built-in dictionary that allows accessing dictionary keys as attributes like JavaScript object.

Plain Text
pip install easydict
Python
from easydict import EasyDict as edict

my_dict = edict({
    "name": "John",
    "age": 30,
    "location": {
        "city": "New York",
        "state": "NY"
    }
})
print(my_dict['name'])  # Output: John
print(my_dict.name)  # Output: John
print(my_dict.location.city)  # Output: New York
Bash
type(my_dict) # easydict.EasyDict

Conversion Back to Dictionary

Python
normal_dict = dict(my_dict)

Normal dictionary::

Bash
data = {
    "name": "John",
    "age": 30,
    "location": {
        "city": "New York",
        "state": "NY"
    }
}
data["location"]["city"] # New York 
data.location.city  # 'dict' object has no attribute 'location'

Keep building your data skillset

Explore more SQL, Python, analytics, and engineering tutorials.