Hi, and welcome! Today, we'll explore HashMaps, a data structure that organizes data as key-value pairs, much like a treasure box with unique labels for each compartment.
Imagine dozens of toys in a box. If each toy had a unique label (the key), you could directly select a toy (the value) using the label. No rummaging required — that's the power of HashMaps! Today, we'll understand HashMaps and learn how to implement them in Python.
HashMaps are special types of lists that utilize unique keys instead of indexes. When you know the key (toy's label), you can directly pick up the value (toy). That's how a HashMap works!
Consider an oversized library of books. With HashMaps (which act like the library catalog), you'll quickly locate any book using a unique number (key)!
Python implements HashMaps through dictionaries. They hold data in key-value pairs, enclosed within {}
(curly braces).
Let's create a dictionary, functioning as a catalog for a library:
In this dictionary, book1
, book2
, and book3
are keys, while the book titles serve as their respective values.
It's important to remember that while dictionary values can be mutable or immutable types, dictionary keys must be of an immutable type (such as strings, numbers, or tuples).
