HashMaps (aka: Dictionaries) in Python
Introduction
Java has a built-in called HashMap
. It allows you to store and very quickly retrieve key value pairs.
In Python, this is called a dictionary
and appears very similar to a JSON Object for those familiar with Javascript and similar languages.
Dictionaries as HashMaps
An example of a dictionary in Python is as follows:
|
|
The same can be created using the dict
keyword:
|
|
A third way to create and populate this:
Accessing values
As per our last creation option, accessing is much the same.
Deleting a value
Deleting a value is as simple as calling the key with the del
keword.
|
|
This will result in the following output: