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__














Shrimant TelgavePosted Oct 3, 2019, 9:27 AM
Nice article. But just one suggestion from my side please add the code as well instead of adding screenshots only. That will help people to practice easily. Thank you.