Dictionaries
- Dictionaries are another type of collection of data values in Python.
- It is a kind of mapping, i.e., it stores the elements in the form of key-value pairs that associate the keys to values.
- Dictionaries are Mutable, i.e., changeable.
- It is depicted within - { }
- Each key-pair value is separated by a comma (,).
- Every value to its corresponding key is given by a colon (:).
- Dictionaries are unordered sets of key-value pairs so their values can have references to any type of object. Dictionary is a Collection.
- Dictionaries are indexed by keys. Keys must be of immutable types.
- The Key-value pairs of dictionaries are associated with one another with some internal function called Hash Function. This way of linking is called Mapping.
NOTE
The keys must be of a
hashable type, which means that they must have a hash value that never changes during the key’s lifetime.
Creating a Dictionary
Nested Dictionaries
A dictionary within a dictionary is called a Nested Dictionary. This means that you can add dictionaries as values inside a dictionary.
Keys and values of the dictionary can be of any type - integer, string, or even, a list or a tuple.
If we try to access an element (key or value) which does not exist, the compiler gives you an error.
Dictionary Constructor
Dictionary has its own constructor, dict(); we can use this constructor to create a new dictionary.
Traversing a Dictionary
Another way to iterate/traverse through items in a dictionary is to use the items() method which we will discuss further under the topic- Dictionary Methods.
Adding elements to a Dictionary
Another way to add items is by using __setitem__
Updating elements in a Dictionary
Another way to update elements in a dictionary is to use method update(), which we will discuss further under the topic- Dictionary Methods.
Deleting elements of a Dictionary
Using del()
Using pop()
You can also clear the whole dictionary by using clear().
DICTIONARY METHODS
Len() method
This method returns the length of the dictionary, i.e., the count of the elements (key-value pairs).
Clear() method
This method removes all items from the dictionary and the dictionary becomes empty.
Get() method
This method returns the value of the corresponding key entered in the argument. If the key doesn’t exist, it pops up with an error.
Items()method
This method returns all the items of the dictionary as a sequence of (key, value) tuple.
Keys()method
This method returns all the keys in the dictionary as a sequence of keys (in a list).
Values()method
This method returns all the values in the dictionary as a sequence (a list).
Update()method
This method merges key: value pairs from the new dictionary into the original dictionary.
Summary
In this article, we discussed all the methods of Dictionary and learned how to create access, traverse, add, update, nest or delete elements in dictionaries. I hope this will help the readers to understand how to use and implement Dictionaries in Python.
Feedback or queries related to this article are most welcome. Thanks for reading!!