Python equivalent for HashMap

112,199

You need a dict:

my_dict = {'cheese': 'cake'}

Example code (from the docs):

>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> a == b == c == d == e
True

You can read more about dictionaries here.

Share:
112,199
Wolf
Author by

Wolf

I'm a masters Student at the University at Buffalo pursuing my masters in Computer Science. Currently working in intel as an intern.

Updated on July 09, 2022

Comments

  • Wolf
    Wolf almost 2 years

    I'm new to python. I have a directory which has many subfolders and files. So in these files I have to replace some specified set of strings to new strings. In java I have done this using HashMap. I have stored the old strings as keys and new strings as their corresponding values. I searched for the key in the hashMap and if I got a hit, I replaced with the corresponding value. Is there something similar to hashMap in Python or can you suggest how to go about this problem.

    To give an example lets take the set of strings are Request, Response. I want to change them to MyRequest and MyResponse. My hashMap was

    Key -- value
    Request -- MyRequest
    Response -- MyResponse
    

    I need an equivalent to this.

  • NoName
    NoName over 4 years
    So internally dict works the same as a hash map/hash table?