A dictionary is a container that stores Python objects and each item of the dictionary consist of a key and value. Python dictionaries are also known as associative arrays or hash tables. Curly brackets distinguish a dictionary from other objects. The following example shows a dictionary with three items.
Figure 1
Items in a dictionary are separated by commas and each key is separated by a colon from its own value. The keys of a dictionary should be unique while values may not be. Keep in mind that values of a dictionary can be of any data type, but the keys must be of an immutable data type. How can one access dictionary values? To access dictionary values you need the key that belong to the specific value and use square brackets like shown in Figure 2.
Figure 2
If you want to find how many items are there in a dictionary you can find that by using the len() Python built-in function. An example of finding dictionary length is shown in Figure 3.
Figure 3
Sometimes, when you program in Python you may want to store dictionary items in a list. The dict_name.items() method comes in handy in this kind of situation. This method returns a list with all the dictionary items where each item is inside a tuple. You can learn how to use this useful method from Figure 4.
Figure 4
There are many other useful methods which can be useful to you while you program in Python programming language. How about deleting a dictionary or some items of a dictionary? You can do that with the del statment, to delete the entire dictionary use del and after that the name of the dictionary. If you want to delete an item from the dictionary you can do that with the del statement and after this statement you have to put the dictionary name and the key inside square brackets, like you want to access the value of this item. Figure 5 shows you how to delete the entire dictionary and delete dictionary items with the del statement.
Figure 5
This first article about dictionaries in Python is enough for beginners in Python programming language. After we have finished with introducing basics of Python programming language we will move into more advanced stuff.