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?

2

3 Answers 3

4

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

map = {}
map["master"] = "of puppets"
map.get("master")
# Output: 'of puppets'
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.