Hashmaps [python]

Hashmaps [python]

python dictionaries

Introduction

  • In this article we will be talking about all you need to know to get started with the dictionary data structure.This could be likened to a hashmap in other programming languages.
  • Dictionary store data in the form of key:value pairs.
  • From python 3.7 dictionaries are ordered in that the items retain the order in which they are created, and that order will not change, just like lists.
  • Values in dictionaries are accessed using the keys they are associated with.
  • Dictionary keys are unique and immutable. This means a single key cannot appear more than once in the same dictionary and once created , they can not be altered.
  • Not all data types are fit to be used as keys for python dictionaries as some are immutable (lists for that matter).
  • They (keys) have a lot in common with the set, another important inbuilt python data structure.

    Creation of a dictionary

  • Two ways to create a dictionary
    1. Using curly braces : This is the most common way to create a dictionary in python. Screenshot from 2022-10-21 22-58-47.png
    2. Using the dict() constructor : Screenshot from 2022-10-21 22-59-43.png

Why use dictionaries?

  • Speed : Dictionaries are very fast in terms of retrieving data. This is because they are implemented using hash tables.
  • They are mutable in that they can be changed after they have been created.
  • Still on the same point, this makes them the perfect candidate for storing ever changing data size as they can grow and shrink on the fly.
  • They are indexed which refers to their values providing access by keys.
  • Though they do not have sorting method built into them , there are ways to sort dictionaries using the same methods that work on lists.

Accessing values in a dictionary

Screenshot from 2022-10-21 23-02-15.png

  • If the key is not found in the dictionary, a KeyError is raised. The square bracket notation is the same as that used with the list data structure where you pass on the index of the item in question , but here the key takes the place of the index.
  • The get() method is another way to access values in a dictionary. This method returns the value for the specified key if key is in dictionary. If not, it returns the default value None.

Screenshot from 2022-10-21 23-03-57.png

  • The get() method takes two parameters: key and default value. The default value is optional and is None by default.

Screenshot from 2022-10-21 23-04-53.png

Adding values to a dictionary

  • Values can be added to a dictionary using the square bracket notation [].

Screenshot from 2022-10-21 23-05-49.png

  • The update() method adds element(s) to the dictionary if the key is not in the dictionary. If the key is in the dictionary however, it updates the key with the new value.

Screenshot from 2022-10-21 23-08-20.png

  • The update() method takes either a dictionary or an iterable object of key/value pairs (generally tuples).

Screenshot from 2022-10-21 23-10-31.png

Removing values from a dictionary

  • Values can be removed from a dictionary using the pop() method.
  • The pop() method removes the item with the specified key and returns it.

Screenshot from 2022-10-21 23-16-51.png

  • The pop method deletes the key specified in the parentheses as well as the value associated with it and returns that value. If however the key is not found in the dictionary and the second argument was not specified, It throws an error.
  • When a second argument is passed to pop , it returns that argument in case the specified key is not found in the dictionary.
  • The del keyword removes the item with the specified key name but unlike pop, does not return the corresponding value.

Screenshot from 2022-10-21 23-22-03.png

  • It could also be used to delete the whole dictionary.

Screenshot from 2022-10-21 23-23-00.png

  • After deleting the entire dictionary, trying to acces it again throws an error.
  • The clear() method empties the dictionary.

Screenshot from 2022-10-21 23-24-27.png

Looping through a dictionary

  • A dictionary can be looped through using a for loop.
  • When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.

Screenshot from 2022-10-21 23-26-52.png

  • It is also possible to loop through both keys and values, by using the items() method.

Screenshot from 2022-10-21 23-28-19.png

Copying a dictionary

  • There are two ways to make a copy of a dictionary.
  • The copy method:

Screenshot from 2022-10-21 23-32-43.png

  • Using the dict constructor: Screenshot from 2022-10-21 23-33-11.png

Nested dictionaries

  • A dictionary can contain dictionaries, this is called nesting , it can be done in lists too.

Screenshot from 2022-10-21 23-36-14.png

Conclusion

  • In this article we have talked about all you need to know to get started with the dictionary data structure.
  • We have talked about the creation of a dictionary, the different ways to access values in a dictionary, adding values to a dictionary, removing values from a dictionary, looping through a dictionary, copying a dictionary and nested dictionaries.
  • This is only a tea spoonful from the ocean of things one could do with the dictionary. More information about them I recommend could be got from the official python documentation..
  • Thanks for reading , Hope you did find this article helpful. please leave a comment and reaction .... :)