Hi there! Now, we will cover our last data collection type called the dictionary data type. In this article, we will cover what is a dictionary in Python, how to define a dictionary, dictionary operations and many more.
Create a Dictionary
First of all, let’s understand what is the dictionary. The dictionaries are a collection of key-value pairs. The definition of the dictionary is an ordered, changeable, indexed data collection type in Python. And also, dictionaries do not allow storing duplicate values. The dictionaries are created by using curly braces {}
and have keys and values.
|
|
Access elements of a Dictionary
Here, you can see how we create a dictionary. The main advantage of the dictionary we can quickly access to data is required. Because dictionaries consist of programmer-defined indices called keys, not consist of numerical indices. You can access the values of a dictionary using that keys. For that, you can mention the variable name of the dictionary and put the key inside the square brackets like below,
|
|
Also, we have another method to access values in a dictionary. The get()
function can be used to get value according to the key. You can pass the key within the parentheses in the get()
function.
|
|
We can get a list of the all keys in the dictionary using the keys()
function. And also, the values()
function can use to get a list of all the values in the dictionary. Now, let’s try those functions,
|
|
The dictionary object has another useful function called the items()
. That function is used to convert a dictionary into a list of tuples.
|
|
Operations of Dictionaries
Add an Element
Alright! Now we are going to learn about operations in dictionaries such as adding elements, updating a dictionary, removing elements, and dictionary iteration. First, let’s look at how to add elements.
|
|
Update an Element
And also, we can update an element like the below code,
|
|
We can do the above operation using the update()
function. Here, we have two methods. First is passing the updated elements as a dictionary inside the parenthesis to update the dictionary. The second is updating the dictionary by assigning.
|
|
Delete an Element using del
keyword
To remove an element, we have many methods. First, let’s look at how to do it using the del
keyword.
|
|
Delete the Dictionary
If you want to delete the entire dictionary, you can do it like this,
|
|
Delete an Element
The pop()
function and the popitem ()
function can use to remove an element in the dictionary. The pop()
function can use to remove a specific element in the dictionary. And the popitem()
function is used to remove the last element of the dictionary.
|
|
Clear the Dictionary
Suppose, you need to remove all the elements in the dictionary. For that, the dictionary object has a clear()
function. You can clear the dictionary using that function.
|
|
Iterating through a Dictionary
Now, we will learn about iteration operations in a dictionary. We have four methods to iterate through a dictionary.
Using a for loop
1 2 3 4
x = {"Name": "Savindu", "Age": 20, "Country": "Sri Lanka"} # define a dictionary for i in x: print(i, x[i])
Using the
enumerate()
1 2 3 4 5 6 7 8 9
x = {"Name": "Savindu", "Age": 20, "Country": "Sri Lanka"} # define a dictionary for i in enumerate(x.items()): print(i) # output # (0, ('Name', 'Savindu')) # (1, ('Age', 20)) # (2, ('Country', 'Sri Lanka'))
Here, the enumerate()
function is that it returns a tuple with the counter and value. We need to pass x.items()
inside the parenthesis.
Using the
items()
method in the dictionary Class.1 2 3 4 5
x = {"Name": "Savindu", "Age": 20, "Country": "Sri Lanka"} # define a dictionary for key, value in x.items(): print(key, value)
Using list comprehension.
1 2 3 4 5 6
x = {"Name": "Savindu", "Age": 20, "Country": "Sri Lanka"} # define a dictionary y = [(i, x[i]) for i in x] # Output # [('Name', 'Savindu'), ('Age', 20), ('Country', 'Sri Lanka')]
Now, we have come to the end of today’s article. Lastly, we should concern about which times not to use a dictionary.
- For storing data that shouldn’t be modified.
- When resource (memory) usage is a concern.
By now, we have covered data collection types in Python. In the next article, we will look at data collection type comparison and type conversion. Goodbye!