Dictionaries In Python

This article will help you understand what dictionaries are in Python, how we can create and access one, how to traverse, add, update, nest, or delete elements in dictionaries. Also, you will learn about various methods of dictionaries.

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

 
Dictionaries In Python
 

Nested Dictionaries

 
A dictionary within a dictionary is called a Nested Dictionary. This means that you can add dictionaries as values inside a dictionary.
 
Dictionaries In Python
 
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.
 
Dictionaries In Python
 

Dictionary Constructor

 
Dictionary has its own constructor, dict(); we can use this constructor to create a new dictionary.
 
Dictionaries In Python
 

Traversing a Dictionary

 
Dictionaries In Python
 
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

 
Dictionaries In Python
 
Another way to add items is by using __setitem__
 
Dictionaries In Python
 

Updating elements in a Dictionary

 
Dictionaries In Python
 
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()
 
Dictionaries In Python
 
Using pop()
 
Dictionaries In Python
 
You can also clear the whole dictionary by using clear().
 
Dictionaries In Python
 

DICTIONARY METHODS

 
Dictionaries In Python
Len() method
 
This method returns the length of the dictionary, i.e., the count of the elements (key-value pairs).
 
Dictionaries In Python
 
Clear() method
 
This method removes all items from the dictionary and the dictionary becomes empty.
 
Dictionaries In Python
 
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.
 
Dictionaries In Python
 
Items()method
 
This method returns all the items of the dictionary as a sequence of (key, value) tuple.
 
Dictionaries In Python
 
Keys()method
 
This method returns all the keys in the dictionary as a sequence of keys (in a list).
 
Dictionaries In Python
 
Values()method
 
This method returns all the values in the dictionary as a sequence (a list).
 
Dictionaries In Python
 
Update()method
 
This method merges key: value pairs from the new dictionary into the original dictionary.
 
Dictionaries In Python
 

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!!