As highly competent engineers working in technology and finance, we often come across tools like Redis, a simple yet powerful in-memory key-value store, used for caching, configuration storage, data sharing between threads, and more. Let's try to build our own rudimentary version of a key-value store.
In Python, a dictionary is a built-in data structure that can be used as a key-value store. It uses hashmaps under the hood and allows us to store and retrieve data using unique keys. Think of it as a simple database. You would have seen this being used in programming paradigms like dynamic programming.
To build this key-value store, we're going to use a Python dictionary. We are creating a kvstore, which stands for key-value store, as an empty dictionary - kvstore = {}
.
You can add key-value pairs to the kvstore with the syntax kvstore[key] = value
. For example, kvstore["AMZN"] = "Amazon"
adds the key-value pair "AMZN" : "Amazon"
to our kvstore.
Give it a shot below by executing the following python code.
xxxxxxxxxx
if __name__ == "__main__":
# Define Key-Value Store
kvstore = {}
# Add Key-Value Pairs
kvstore["AMZN"] = "Amazon"
kvstore["MSFT"] = "Microsoft"
kvstore["GOOGL"] = "Google"
print(kvstore)