Dictionaries



Overview



• Are analogous to "hashes" in some other languages
• A data structure that stores simple key-value pairs
• Syntax: {key: value}
• Key
◇ Restriction: whatever you want to use as a key has to be immutable and hash-able
◇ The following data type can be used as a dictionary Key:
▪ tuple
▪ int
▪ float
▪ bool
▪ str
▪ bytes
• Values
◇ Can be anything
◇ Type don't have to be consistent
• Dictionaries store “unordered” key-value pair. If you want to store ordered key-value pair, use OrderedDict collection

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example



# Dictionaries
>>> d = {"apples"5"pears"2"oranges"9}
>>> d["pears"]
2

>>> d["pears"] = 6
>>> d["bananas"] = 12
>>> d
{'apples'5'pears'6'bananas'12'oranges'9}


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Other Dictionary Methods



https://docs.python.org/3/library/stdtypes.html#typesmapping


Index