Back

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

 

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")

1 Answer

0 votes
by (106k points)

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

streetno = {"1":"Hello Worls", "2":"World", "3":"Hello", "4":"abcd","5":"lpg"}

as well as using the dict keyword:

streetno = dict({"1":"Hello Worls", "2":"World"})

or:

streetno = {} 

streetno["1"] = "Hello Worls"

To know more about this you can have a look at the following video tutorial:-

Related questions

0 votes
1 answer
asked Mar 27, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Aug 24, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories

...