[011] Introduction to Python Basics: Dictionary Keys, Dictionary Methods

Koay Yong Cett
7 min readJul 6, 2020
Photo by Pisit Heng on Unsplash

Well, a dictionary of values can hold any sort of data type but what about the keys?

Dictionary Keys:

  • Until now we only use strings to denote a key. How about we change the strings to other data type.
  • If we changed the strings to numbers, it still valid/work and yielded a result.
  • What about True? Yes, It look like it is working.
  • What if we have a list of 100 and will it works? No, it yields an error of unhashable type list. What that this mean?
  • Dictionary keys need to have a special property where it needs to be immutable. This means that the key cannot be changed like the numbers and boolean that we mentioned previously.
  • Remember that the string is immutable while a list can be changed. For instance, we can reassign the index of zero to something else.
  • Thus, the dictionary will says that my keys which are storing in the memory and don’t want to lose them. Then, the keys has to be something that doesn’t change in order to keep the values safe.
  • For this reason, the dictionary keys have to be immutable. Most of the times, the keys used will be something descriptive like the string we mentioned before.
  • What will happen when we search for ‘123’ since there are two ‘123’? The key in the dictionary has to be unique because there can be only one key and that key is going to represent a values in the memory space.
  • Whenever, we do the same key and add a value, then it is going to overwrite. Then, the original [1,2,3] list is no longer exist. A key has to be unique and it’s something that can only exist once. Otherwise, we overwrite it and the result appears as hello.

Dictionary Methods:

  • Here, we are able to access to the basket key. However, what if we don’t know what the dictionary is.The dictionary(user) maybe located at somewhere else where we can’t see.
  • We want to know whether the user has property like age. Therefore, does the user has the age key?
  • When we try to run the code, we get an error know as key error. The error will stop the program as soon as python interpreters received an error and ignore any other code that happen underneath the error line as the execution is stop.
  • There is another way to access a key and see whether if the property is exist which is the dot get (.get). The dot get is a method for the dictionary in Python.
  • We get none in the result as the age is not exist which is the absence of value. It also doesn’t produce any error.

Let’s say there is no age in the user but you want to have a default value. We can do something like this:

  • Here, we grab the age in the user dictionary. If the age doesn’t exist on the user dictionary then use the 55 given.
  • The result is 20 as the age is exist in the user dictionary. The 55 will only be used when the age is absence in the user dictionary.

Another way to create dictionary that is not so common:

  • This is the not so common way to create a dictionary. Be aware of it.

Well, there is one more way that we have actually seen in the list on how to look for an item in the dictionary:

  • Remember the ‘in’ where it is used to check whether the ‘i’ is in the string/list.
  • We can also do the same for dictionary using the ‘in’. The basket is exist in the dictionary since the True is yielded.
  • here, we can see that the size is not exist and False is yielded.

Other interesting Python Dictionary Methods:

https://www.w3schools.com/python/python_ref_dictionary.asp
  • Here, we notice that there is not that many dictionary methods that the Python have.

Keys():

  • It will simply checks the keys in the dictionary’s keys and return a list that containing the dictionary’s keys. Here, it checks that the age is existed in the user dictionary’s keys.

Values():

  • It will simply checks the values in the dictionary’s keys and return a list that containing tall the values in the dictionary. Here, it checks that the hello is True by going through/iterate over the dictionary.

Items():

  • The methods that is really useful is items(). It is special because we have seen keys and values but items actually grabs the entire item.
  • Here, we seen the dict_items and the list of items. There is something quite new to us and that is tuple. The keys, values and items methods allow us to grab all this information easily form our dictionary.

Clear():

  • It removes all elements from the dictionary. Here, we see that None is yielded mean that the dictionary is empty.
  • This is another way to clear the dictionary and we get {} instead of None. We see that clear method doesn’t actually return anything and just removes whatever dictionary has. This creates an empty {} brackets.

Copy():

  • It will return a copy of the dictionary. Here we get two similar users dictionary where the user2 gets all the same keys and values from user.
  • Here, the first user is copy and second user has the old information since that it copy the information before the clear method for the user.

Pop():

  • It will remove the element with specified keys from the dictionary. Here, the pop method returns the value of whatever that got removed.
  • The age does not exist anymore in the dictionary since we pop it off the dictionary. The pop method actually removes the actual value or returns the actual value 20.

Popitem():

  • It will removes the last inserted key value pair and in this case is the age. Thus, the last item on the dictionary gets removed.

Update():

  • It will simply update the dictionary with specified key values. Here, the age got updated.
  • If the key like ages doesn’t exist and it will still update with a new key item.

Thanks for reading my stories 😃

--

--

Koay Yong Cett

A Bachelor CS student with major in Network Security (UniSZa). Every stories I shared is based on my personal opinion. Thanks you. Having my Internship now.