Have you ever got stumped when you tried assessing an item in a list or dictionary because you received a result which you never expected? Some would say they found a bug. Truly, that bug rests in the fact that you did not understand the subtleties between types when using a list or dictionary.
Take this little program that shows the subtleties in accessing items in a dictionary.
The dictionary has three items, with the keys being ints and the values as strings. That is the first difference you should notice. This is just illustrative because keys of dictionaries can also be any immutable type. On line 3, I asked the interpreter to tell me if 10, int type, was a key, and if ‘10’, string type, was a key. It says True for the former and False for the latter. This is a problem I encounter while teaching on a daily basis. Many persons tend to confuse the int for the string and vice versa. They are different values.
Then comes accessing the items in the dictionary. On line four, I inserted 10 as the key to the dictionary. Note this: as key and not the index. Python reported the string, ten, as I expected. But notice what happens on the line 5 when I used the string ‘10’ as the index. Python gave a KeyError.
So, this shows that types really matters when you are accessing dictionaries. The same goes for lists.
How to find items in a list and dictionary.
Imagine we have a list of names.
When you run it, you should notice that the int 1 is not in the list. I could have used some other value, maybe ‘one’ or maybe ‘Rose’ but the "in" operator shows us whatever is in the list. One problem I have encountered with most of my students is that they don’t understand the subtleties of the in operator. What I always tell them is to consider it as going through each item in the list and looking for a match. If it returns a match, it returns True, and if not it returns False.
Now, what if we mistake an int for a string or vice versa in the list. Let’s take another list. This time of years.
If you run it, what it prints out is:
2019 in list.
2019 is not in list.
What is going on? It’s just simple. The items in the list are strings but on line 10 we gave it an int. So, when programming you have to be very careful not to be carried away and confuse an int for a string and vice versa.
What about indexing and keys.
What we expect is that when we give the interpreter something like variable_name[index]
when it is an iterable, we should expect to get an item in return. But what item we get depends on the data structure we are using. If variable_name is a list, we will get an item in the list with that index. But if variable_name is a dictionary, what we get is a value of the key, index.
That gets many people confused sometimes. Well, it is all about knowing that lists and dictionaries are arranged differently. A list contains references to items of value and each item is indexed from zero. For example, let’s take our list of names again.
names = ['david', 'michael', 'daniel']
The list has three items which are referenced through an index. 'david' is at index 0, 'michael' is at index 1, and 'daniel' is at index 2. So, we can access the items through the indices. For example this command, names[0]
will return david, while names[2]
will return daniel.
But this is different with dictionaries. Let’s take a dictionary of ints as keys and strings as values for example.
num_dict = {10 : 'ten', 20 : 'twenty', 30 : 'thirty'}
If we run the statement num_dict[0]
we will get a KeyError. You know why? Because dictionaries accesses its values based on the keys and 0 is not a key in the num_dict dictionary. So, don’t confuse this with that of lists. To access the second item in the dictionary which is ‘twenty’, we need to get it through its key, and the key is an int, 20. So we run the statement thus: num_dict[20]
which returns ‘twenty’.
Note that keys of dictionaries can only be immutable types, including sets, while values for dictionaries can be of any type.
I hope you enjoyed this article. If you like receiving more solved problems like this from this blog, just subscribe and it will be sent to your inbox the moment I publish a new article.