Introduction
Another article which is discussing on of a specific collection data type exist before. This article is also discussing about how to use a specific collection data type. As the main content, the collection data type for further discussion is dictionary. So, in this article, there will be specific content about dictionary starting from the declaration and the definition of it. Furthermore, it will also describe in detail about how to add, remove and also replace data inside dictionary.
Using Dictionary in Python
So, this part will discuss about how to use dictionary in Python programming language. Actually, the usage for dictionary ranging from declaring or defining to adding, removing and replacing data inside of it. It will also provide some useful examples along the way accordingly.
Declaring and Defining Dictionary
As usual, in this context, there is two types of declaration or definition for dictionary. The first one is the declaration or the definition for dictionary without having to include any data at all. Just an empty dictionary as it exist below :
dictionary_example = {}
On the other hand, for declaring or defining using dictionary collection data type, it will need a key in the form of a string to be able to act as a reference for the data. Below is the example for the declaration and the definition :
dictionary_example = {'id':1,'Name':'Alfred','Age':34,'Country':'England','Height':1.85,'Weight':70}
Adding Data in Dictionary
So, the first one after successfully declaring or defining to use dictionary collection data type, the most basic aspect or process is about adding data. In other words, adding data or an element to the dictionary. The following is an example for adding not only the data but also the key which acts as the reference. Below is the example for adding it in a sequence starting from the empty declaration of dictionary :
>>> dictionary_example = {} >>> dictionary_example.update({'id':1}); >>> print(dictionary_example); {'id': 1} >>> print(dictionary_example);
As the execution of the above command for adding data into the dictionary, it is using method with the name ‘update’. It is using pair key and value as the parameter enclosed with curly bracket. In other words, it actually pass dictionary as the parameter for adding data to the dictionary. Because a dictionary itself, it use pair key and value with curly brackets enclosing it.
Removing Data in Dictionary
After successfully adding data in a dictionary, the last aspect of the processing is for removing data in it. So, how to remove data from it ?. The following is an example for removing data from dictionary :
>>> dictionary_example.pop('id'); 1 >>> print(dictionary_example); {} >>>
In the example above, it is very clear that removing data is possible using the method exist in the dictionary. it is a method with the name of pop. Moreover, it is acquiring key as the parameter for the method. As an example, it is a method with the name of pop and it has a parameter with the name of ‘id’. Just choose any available parameter in the dictionary. In the previous example, it has a dictionary where it has ‘id’ parameter. So, just remove the data exist which has the ‘id’ key.
Replacing Data in Dictionary
It is possible for replacing data in dictionary. As an example in this context, it is replacing just the value of the data. Not replacing the key but just the value. So, instead changing the key, this example is using replacement aspect in dictionary for the data. Below is an example with series of process with dictionary from starting to check the content until adding and also replacing data in it :
>>> print(dictionary_example); {} >>> dictionary_example.update({'id':1}); >>> dictionary_example.update({'name':'Alec'}); >>> dictionary_example.update({'country':'Austria'}); >>> print(dictionary_example); {'id': 1, 'name': 'Alec', 'country': 'Austria'} >>> dictionary_example.update({'weight':90.5}) >>> print(dictionary_example); {'id': 1, 'name': 'Alec', 'country': 'Austria', 'weight': 90.5} >>> dictionary_example.update({'weight':80.5}) >>> print(dictionary_example); {'id': 1, 'name': 'Alec', 'country': 'Austria', 'weight': 80.5} >>>
As the one exist in the part for adding data into the dictionary which is using ‘update()’ method, replacing data is also using the same method. Specifically, just redefine the data by referring to the key. In the above example, there are two similar lines with different assignment data or value. Those are exist as follows :
>>> dictionary_example.update({'weight':90.5}) >>> print(dictionary_example); {'id': 1, 'name': 'Alec', 'country': 'Austria', 'weight': 90.5} >>> dictionary_example.update({'weight':80.5}) >>> print(dictionary_example); {'id': 1, 'name': 'Alec', 'country': 'Austria', 'weight': 80.5}
The first assignment basically just add a data for the key ‘weight’. Later on, there is another assignment which is aiming to replace the data with the key of ‘weight’. Fortunately it is a success and the value of the data with the key ‘weight’ is changed.