Let us see how to format containers that were accessed through __getitem__ or getattr() using the format() method in Python.
Accessing containers that support __getitem__
a) For Dictionaries
Bill Gates Steve Jobs
f[Microsoft] is replaced by Bill Gates and f[Apple] is replaced by Steve Jobs.
b) For lists
Accessing attributes on objects that support getattr()
a) For Class
p.language is replaced by Python as language is an attribute of Program
Accessing the nested structure
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course