A Dictionary in Python contains a value with its key.
Dictionary is different from the other type of sequences in Python like Lists, Tuples, etc.
From the creation to accessing values, a Dictionary is somewhat different than other sequences but it does share some of the basic “sequential” properties with them too.
There are some advantages associated with Dictionaries over the other type of sequences: –
- Dictionaries have this advantage of having a key associated with the values because when accessing values, you don’t have to know its index number.
- Also, you can check whether a key or its value exists or not.
A Dictionary is a mutable sequence which means that it can be changed or updated. It is unordered but indexed, i.e., items can be accessed but are not in order.
The value and its key in a Dictionary are separated by a colon (:). All of the key – value pairs are separated by a comma and everything is enclosed in curly braces.
A set of values can be associated to a single key like this: –
Moreover, the constructor dict() can also be used to create a dictionary like this: –
Let’s see how you can access values in a Dictionary.
Values in a Dictionary are accessed by their Keys.
Values with no keys cannot be accessed. If you try to access a value with a key that does not exist in the dictionary, you will get an error.
Similarly, values can be accessed by a method called get() by passing valid keys as an argument.
An example in the “Creating Dictionaries” section showed how you can associate multiple values to a single key. Let’s see how you can access the values: –
As already discussed, items from a Dictionary can be changed or removed or more items can be added in a Dictionary.
Values can be added easily to a Dictionary. A single value can be added with the help of its key.
You can also add multiple values to a single key like this –
There are many ways to remove items from a Dictionary. The method pop() removes a specific key with its value. However, the popitem() method removes the last inserted key-value pair from the dictionary. However, in the versions older than Python 3.7. It was used to remove a random key-value pair and return it.
The function del was used to remove the whole dictionary and it can also be used to remove a particular key-value pair.
Changing Item Values
Item values can be changed by using the key of that particular value.
A dictionary can contain multiple dictionaries inside it. This type of dictionary is called a Nested Dictionary. You can create a Nested dictionary like this: –
You can also add multiple separately created dictionaries into a single nested Dictionary.
Some Useful Methods/Functions
Let us see, how you can use multiple useful functions to get most functionality out of Dictionaries.
Check the length of a Dictionary
The method len() can be used to get the length of a Dictionary.
Empty a Dictionary
The method clear() can be used to delete all the items from a Dictionary.
Copy a Dictionary
Dictionaries can be copied by using the method copy(). This can be done like this: –
Using a “for” Loop
A for loop can be used to get the keys and values from the dictionary. Looping simply through a dictionary will only return keys but values can also be obtained with help of some methods.
The above code will only print keys from the dictionary. To get the values from the dictionary, this method can be used: –
Both keys and values as key-value pairs can also be extracted using tems() method.
We have already talked about some of the functions/methods, here is a table of all of the functions that can be used for Dictionaries: –
|get()||This will return the value of a particular key that is passed as an argument in the function.|
|keys()||This returns all the keys from the dictionary.|
|values()||This returns all the values from the dictionary.|
|pop()||This removes the value associated with key that is passed as an argument in the function.|
|popitem()||This removes the last element from the dictionary. However, this is not true for the versions of Python older than Python 3.7. In the older versions, it removes and returns an arbitrary key and value pair.|
|update()||This updates the dictionary with a particular key and value pair.|
|setdefualt()||This will return the value of the key. If there is no key present, it will update the dictionary with a pre-specified key and value pair.|
|items()||This will return a list that will contain all the key and value pairs present in the dictionary.|
|copy()||This returns a copied dictionary.|
|fromkeys()||This will return a dictionary with particular keys and their values.|
|clear()||This function deletes all the elements from a dictionary.|