Explain the working of Map and Hash Map in Python

Question

As mentioned earlier, the level of Python questions has increased in the recent interviews and we suggest you to solve at least 50-100 good quality questions.

Define Hash map and map using simple example. Use lambda function if needed

 

in progress 0
TheDataMonk 3 years 2 Answers 442 views Grand Master 1

Answers ( 2 )

  1. A hash map is a data structure and a collection of key-value pairs – for example a dictionary.
    d= (‘a’:1,’b’:2,’c’:3} where a,b,c are keys and 1,2,3 are values corresponding to the keys.

    A map function is used to apply a function across all rows of a series or a tuple or a list or even all key-value pairs of a dictionary. It “maps” the function across them.

    def length_fun(str):
    return len(str)
    x = map(length_fun, (‘apple’, ‘banana’, ‘orange’))
    print(list(x)) – 5,6,6

  2. Map:
    It is a built in function that allows to process and transform all the items in an iterable without using an explicit for loop. It is useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable.
    Syntax:
    Map(function,iterable)
    E.g:
    number=[1,2,3,4]
    result=list(map(lambda X:X*2,number))
    print(result)

    Hashmap:
    In simple words, hashmaps can be thought of as a currency. Now, every currency or note has a value attached to it. The note also has its unique identity code. Let’s call this unique id a key. This key will hold some value for the currency. So, in order to determine the value of the currency, one must know the key number and only then, the value of the currency will be fetched. Hashmaps operate in a similar fashion.

    Hashmaps stores data in these key and value pairs. One object is used as a key to determining the value of another object. On insertion of the same key twice, the most recent insertion is stored, or the value of the key is replaced with the most recent edition. Hashmaps are unsynchronised. It means any addition or deletion of (key, value) pairs are done in a random manner. Also, there can only exist one null key in hashmaps.
    Hash maps in Python are implemented through the built-in dictionary data type.

Leave an answer

Browse
Browse