Explain the working of Map and Hash Map in Python
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 2 )
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
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.