Python Dictionary and JSON — A Comprehensive Guide
Python dictionary is a a collection of key-value pairs. Dictionary is mutable(can be changed), unordered and can be indexed (using the keys). Despite the fact that dictionary is mutable the keys are not. In Python, dictionary is of dict
data type and is one of the data structures (in-memory objects). Other data structures in Python includes list, tuple and set.
Remark:
Mutable data types in Python are list, dictionary, set and user-defined classes.
On the other hand, immutable data types includes int, float, decimal, bool, string, tuple, and range.
Here is an example of a dictionary
my_dict = {
'Mathematics' : 145,
'Done': True,
'Lecturer': 'Allan Smith'
}
Mathematics,
Done
, Lecturer
are the keys and 145
,True
and Allan Smith
are the values hence the term key-value pairs.
JSON stands for JavaScript Object Notation is a data format — merely set of rules defining how to represent data in text/string form. JSON, therefore, is just a object and it is of str
datatype in Python.
JSON string and dictionary looks a like except for the datatype (In Python dictionary is dict
and JSON is a str
) and the following cases