You can think of Dictionaries as a collection of data that is unorganized, unlike lists. Items in dictionaries are stored and fetched by a key, instead of a positional offset. Dictionaries can grow and shrink and can contain any objects at any nested level. They can also be changed in place. However, dictionaries do not support the sequence operations that work on strings and lists, such as slicing and concatenation, etc. They map keys to values. Dictionaries are written as {key:value} pairs, separated by commas.

Here we create a dictionary. The 'fruit' and 'veggie' are the keys and the 'apple' and 'celery' are the values. We use the square bracket to index dictionaries by key as we did to index lists by offsets. The left to right positional order does not matter as it will change due to its unordered collection. The first key:value pair, for example, may not always be the same.

Here is an example of other ways to create dictionaries. All 4 make the same dictionary. The first is good if you can spell out the whole dictionary ahead of time. The second is good if you need to make one field at a time on teh fly. The third however requires all keys to be strings. The fourth is good if you need to build up keys and values as sequences at runtime.

The method keys() returns all keys in the dictionary. The method values() returns all the values. The method items() returns a tuple of the (key,value) pair. These methods make it very easy to loop through all keys and find a specific key, for example, and get the value of that key.

Here we add a nested dictionary and use the same methods as we did before. You will notice that the keys() do no show the nested keys. You will need to use a loop to go into the nested dictionary to get the ame results with the nested one. However the values() method will have a value as the nested dictionary. The items() will show tuple form.

dict.get()Trying to call a dicitonary key that does not exist will raise a KeyError. You can either use a try/except to catch the error or you can use the get() method. The get method will 'get' the value of the key and if it does not exist, will return the value None.

dict.fromkeys()Provided you want all the same values for each key, you can use the fromkeys() method to create a dictionary with keys all the same. Hover a for loop would do the same and be more productive and adaptable.