Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I want to implement a HashMap in Python.

I want to ask a user for an input. Now, 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", "ABC");

   streetno.put("2", "DEF");

   streetno.put("3","GHI");

   streetno.put("4","JKL");

   streetno.put("5","MNO")

1 Answer

0 votes
by (25.1k points)

In python we do have HashMaps but they are called dictionaries. A dictionary is a key-value pair that supports fast lookup, just like a Hash Map. There are several ways to use dictionaries:

d1 = {'key1': 'value1', 'key2': 'value2'}

d2 = dict({'key3': 'value3', 'key4': 'value4'})

d3 = dict()

d3["key5"] = "value5"

If you wish to get a more in depth understanding of python you can watch this YouTube video:

Related questions

0 votes
1 answer
asked Aug 24, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Feb 10, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
asked Feb 5, 2021 in Python by laddulakshana (16.4k points)

Browse Categories

...