dict
A dictionary is a mapping type data structure that associates hashable keys
to values
keys
Keys can include immutable values such as numbers
, str
, tuples
, or frozensets
, but must be hashable and unique across the dictionary.
values
Values can be of any or multiple data type(s) or structrues, including other dictionaries, built-in types, custom types, or even objects like functions or classes.
Dictionaries enable the retrieval of a value given the key.
dict
vs list
/ array
Compared to searching for a value in an array
or list
(without knowing the index
position), a dict
uses significantly more space in memory, but has significantly more rapid retrieval. Dictionaries are especially useful in scenarios where the collection of items is large and must be accessed and/or updated frequently.
Declaration
Declaring a dict
decclares an empty dictionary using the literal form:
dict = {}
Initialization
A simple dict
can be initialized using the literal form: