Hash Map in Python

592,560

Solution 1

Python dictionary is a built-in type that supports key-value pairs.

streetno = {"1": "Sachin Tendulkar", "2": "Dravid", "3": "Sehwag", "4": "Laxman", "5": "Kohli"}

as well as using the dict keyword:

streetno = dict({"1": "Sachin Tendulkar", "2": "Dravid"}) 

or:

streetno = {}
streetno["1"] = "Sachin Tendulkar" 

Solution 2

All you wanted (at the time the question was originally asked) was a hint. Here's a hint: In Python, you can use dictionaries.

Solution 3

It's built-in for Python. See dictionaries.

Based on your example:

streetno = {"1": "Sachine Tendulkar",
            "2": "Dravid",
            "3": "Sehwag",
            "4": "Laxman",
            "5": "Kohli" }

You could then access it like so:

sachine = streetno["1"]

Also worth mentioning: it can use any non-mutable data type as a key. That is, it can use a tuple, boolean, or string as a key.

Solution 4

streetno = { 1 : "Sachin Tendulkar",
            2 : "Dravid",
            3 : "Sehwag",
            4 : "Laxman",
            5 : "Kohli" }

And to retrieve values:

name = streetno.get(3, "default value")

Or

name = streetno[3]

That's using number as keys, put quotes around the numbers to use strings as keys.

Solution 5

Hash maps are built-in in Python, they're called dictionaries:

streetno = {}                        #create a dictionary called streetno
streetno["1"] = "Sachin Tendulkar"   #assign value to key "1"

Usage:

"1" in streetno                      #check if key "1" is in streetno
streetno["1"]                        #get the value from key "1"

See the documentation for more information, e.g. built-in methods and so on. They're great, and very common in Python programs (unsurprisingly).

Share:
592,560
Kiran Bhat
Author by

Kiran Bhat

Updated on May 12, 2021

Comments

  • Kiran Bhat
    Kiran Bhat about 3 years

    I want to implement a HashMap in Python. I want to ask a user for an input. depending on his input I am retrieving some information from the HashMap. If the user enters a key of the HashMap, I would like to retrieve the corresponding value.

    How do I implement this functionality in Python?

    HashMap<String,String> streetno=new HashMap<String,String>();
       streetno.put("1", "Sachin Tendulkar");
       streetno.put("2", "Dravid");
       streetno.put("3","Sehwag");
       streetno.put("4","Laxman");
       streetno.put("5","Kohli")
    
  • Admin
    Admin over 12 years
    The second example just builds a dict in the same ways as before and then copies it. The other use dict, which would be more appopriate in this context, is dict(key1=value1, key2=value2, ...) but that requires the keys to strings which are also valid Python identifiers (and internally, this also creates a dictionary).
  • Alan
    Alan over 12 years
    Ah interesting, I didn't realize that naked strings were valid identifiers.
  • Admin
    Admin over 12 years
    I'm not sure if I understand you correctly (what are "naked strings"?), but I believe you got it backwards. Your updated second example is invalid and I never intended to state something like that work. The keyword arguments syntax, which accepts only naked identifiers, internally uses a dictionary. The dict constructor supports keyword arguments and works like def dict(**kwds): return kwds if given keyword arguments.
  • Simon Bergot
    Simon Bergot over 12 years
    second example raises a syntax error. variable names can't start with a number
  • Anu
    Anu over 5 years
    I think your logic is partially correct! hash(key) & 15, 73%15= 13, but it's equivalent: 1001001 & 0001111 = 0001111 i.e 9 and not 13, I think using mod is the correct operation. Correct me if I am wrong!
  • Petro
    Petro over 4 years
    How do you iterate through the list though?
  • F___ThisToxicCommunityHere
    F___ThisToxicCommunityHere over 4 years
    Yeah, it looks like a "map" and it acts like a "map". But the question is not "Map in Python" but "Hash Map in Python": Are dictionaries a hash(!) map?
  • Alan
    Alan over 4 years
    @user2661619 Yes. The underlying implementation of a dict uses a hash function to generate the address location of the item. The implementation is here: hg.python.org/cpython/file/10eea15880db/Objects/dictobject.c
  • pedram bashiri
    pedram bashiri almost 4 years
    I don't understand how this is an answer to the question. What you get here is a dictionary where names are keys and values are some counters (where did you get the input list from anyway?). While the question was looking for a hash map where unique keys point to names as values.
  • Dwa
    Dwa over 3 years
    Why don't you just tell me dictionary is hashmap