Dictionaries¶
What’s in a dictionary¶
Another key basic object in python is the dictionary. Dictionaries are iterables, but unlike lists (and tuples if you got that far) values in a dictionary are indexed by a user specified key rather than a positional argument. the default structure of a dictionary is {key: value}. The value can be any object in python (numbers, strings, boolean, lists, and so on). Keys are a bit more proscriptive; the rules are a bit more complex, but for beginners it’s normally enough to know that numbers and strings are able to be keys. For more details on exactly what python objects can be keys in a dictionary see: Why Lists Can’t be Dictionary Keys. Note as of python version 3.6 dictionaries will remember the order that the data was input, previously they were un-ordered. Now for some examples:
In [1]: my_dict = {'key': 'value'}
In [2]: my_dict
Out[2]: {'key': 'value'}
# to access data from a dictionary you use brackets and the key
In [3]: my_dict['key']