Data Structures
Dictionaries
Dictionaries are data structures that allow you to define custom indexes or keys.
In other languages, dictionaries are referred to as hash maps, hash tables, or associative arrays.
Dictionaries begin and end with curly braces {}
and each key/value pair
is separated by a comma ,
. You define keys and their corresponding values
separated by a colon :
a = {
'name': 'Guido van Rossum',
'yob': 1956
}
Indexing
You can index into a dictionary using the defined keys, which is often easier than remembering numeric indexes
a['name']
a['yob']
Insert
Use the assignment operator =
to insert new items into an existing dictionary
a['job title'] = 'BDFL'
Update
Use the assignment operator =
to update dictionary items
a['job title'] = None
Delete
Use the del
keyword to delete items from a dictionary
del a['job title']