In an effort to continue learning the basic of python it was only a matter of time until I got around to writing a post on dictionaries as a data type option to work with. In python a dictionary is one of several built in data types on top of other options like integers, strings and lists, so they are there to work with right away with the python language itself.
A dictionary is somewhat similar to a list in some ways, as it is also a data structure option, but with a few very important differences. First off unlike a list, a dictionary is a way to create a named collection of values rather than a numbered one. The other important difference is that I can not just loop over a dictionary, at least not a dictionary value by itself anyway.
In this post I will be going over some of the basics of dictionary values in python, how to go about creating them, and looping over the contents of them.
One way to go about creating a dictionary is to use the bracket syntax, when doing so it is also possible to set some starting values for the dictionary also.
Another way to go about creating a dictionary value would be to use the dict built in function. All other built in types have such functions when it comes to lists, and integers, so of course a dictionary value is not an exception with this. When using the dict function as with the bracket syntax it is possible to set some starting values for the dictionary, although with a slightly different syntax.
Another basic thing that I think is important to know right away is how to go about creating a list from a dictionary. There are many reasons why I would want to convert a dictionary to a list, such as when it comes to creating a way to loop over the contents of a dictionary. However there are other options when it comes to that beyond converting to a list, and in some situations it might be better to go with those options. I will be getting into the subject of looping over a dictionary in the next section, but maybe it is a good idea to look over what the options are for getting started with that at least when it comes to creating other forms of a dictionary that will work with loops.
When creating something from a dictionary to loop over there are the key names, and the values, so there should be a way of creating a list of each. If I want a list of key names I can just pass the dictionary value to the list built in function, the return result will be a list of key names. However another option for this would be to use the keys method of the dictionary, this by itself will not return a list, but it can be easily converted to one. If I want a list of value there is the values method of a dictionary, but again I will want to pass the result to the list built in function.
I will be getting into the dict values class and why that is useful later in this post as I think that is a more advanced topic.
Getting the length of a dictionary is more or less the same as getting the length of a list. I just need to pass the dictionary to the len built in function and that seems to give the result I would expect.
Another basic method of a dictionary that I think I should cover here is the pop method of an instance of a dictionary. This method will remove and return the key value pair if found for the given key name as a first argument. When using it a default value should be given as the second argument in the event that the pair is not there, the reason why is because it will cause an Error otherwise rather than giving some kind of default value like None
The clear method of a dictionary does just what the same would suggest. Calling the clear method will remove all of the key value pairs from the dictionary from which it is called off of.
If you try to just go ahead and loop over a dictionary like you would a list you will run into errors. However this problem is easily remedied by converting the dictionary to another from that can be looped over with a for loop, or any other loop, such as by creating a list from the dictionary first.
In this section I will be going over some code options when it comes to looping over the contents of a dictionary.
The items method of a dictionary might be one of the best ways to go about looping over a dictionary, it will allow having both a key and value variable to work with in the body of the loop like this:
Another option would be to use the keys method of a dictionary which will work fine if I just want to loop over the key values of a dictionary. In addition there is also the values method if I just want to loop over values.
Yet another option is the list built in function that will create a list of key names from a dictionary. So then that is also an option if I just want to loop over key names.
Methods like the items method of an instance of a dictionary will not return just a plain old dumb list, but an instance of a special class that is actually a view of the dictionary. So in other words as changes are made to a dictionary to which one of these views is created from that will reflect in the view. If I do truly create a list from a dictionary for example this kind of effect will be lost, and changes will of course not effect that list that is truly separate from the dictionary.
It is a good idea to learn a thing or two about the differences between dictionaries and lists. I have all ready covered the fact that I can not just loop over a dictionary itself, I first need to create a separate list or view object from the dictionary to do that. However maybe there are a few other things that I should point out when it comes to how dictionaries and lists differ.
The thing about lists is not just that they consist of numbered rather than named index values, it is also that those index values are not really static. I guess if I am careful they can be static, but there is of course things that come to mind like sorting a list, or creating a new list from a list that will result in index values referring to different elements. This is one of the reasons why I would want to use a list of course, but in some situations I do not want this kind of effect. Using named keys then is a way to make sure that a given key will always refer to the same value.
That is it for now when it comes to dictionary values in python. I would write more about them, but I am still fairly new to python myself, so maybe I should log some more hours coding examples before getting into expanding this post. Still I think that I have made a good start with the dictionary values in python, there might be a bot more to work out, but for now I think I should move on to other topics, and sooner or later start a real project of some kind with python.