1

I wonder if there is a way of implement hashmap as in java but doing in Python:

for example:

if in java I would do :

HashMap<String, String> map = new HashMap<>();

and then I can put data into the map:

map.put('master' , 'of puppets');

How can I accomplish the same in Python?

tupac shakur
  • 658
  • 1
  • 12
  • 29
  • Does this answer your question? [How to use a Python dictionary?](https://stackoverflow.com/questions/45072283/how-to-use-a-python-dictionary) – sushanth Jun 09 '20 at 14:16
  • 2
    Does this answer your question? [Hash Map in Python](https://stackoverflow.com/questions/8703496/hash-map-in-python) – hradecek Jun 09 '20 at 14:18

3 Answers3

4

Equivalent of hashMap is dict in python.You can do something like,

map = {}
map["master"] = "of puppets"
map.get("master")
# Output: 'of puppets'
3

You've dictionaries in Python

dt = {'name':'xyz','age':30}
print(dt)
dt['name'] = 'abc'
print(dt)
dt['height'] = 6.5
print(dt)

output :

{'name': 'xyz', 'age': 30}
{'name': 'abc', 'age': 30}
{'name': 'abc', 'age': 30, 'height': 6.5}

Python Documentation : https://docs.python.org/3/tutorial/datastructures.html#dictionaries

Strange
  • 1,460
  • 1
  • 7
  • 18
0

dict object is using hash for the keys: " In Python, the Dictionary data types represent the implementation of hash tables. The Keys in the dictionary satisfy the following requirements.

The keys of the dictionary are hashable i.e. the are generated by hashing function which generates unique result for each unique value supplied to the hash function. The order of data elements in a dictionary is not fixed. " https://www.tutorialspoint.com/python_data_structure/python_hash_table.htm

eilon47
  • 41
  • 2