Lists are one of the fundemental data types in Redis. You will often use this data type to manage many features. In this article, we look at many of the common list commands in Redis using Python.
For setting up Redis, I would recommend using a service for you in prod. Azure for example, has a great redis service that scales easily. However, you will want to learn redis and eventually how to scale it yourself. This will help with debugging cloud services or eventually, saving money and not using them.
We will start our intro to redis via using docker compose. Create a docker-compose.yml
file and add the following.
version: "3.2"
services:
redis:
image: "redis:alpine"
command: redis-server
ports:
- "6379:6379"
volumes:
- $PWD/redis-data:/var/lib/redis
- $PWD/redis.conf:/usr/local/etc/redis/redis.conf
environment:
- REDIS_REPLICATION_MODE=master
Ensure you have docker installed and run
docker-compose up
In python, the main used redis module is called redis-py
and can be installed using the follows.
pip install redis
Let's open up a new file, index.py
and go through many of
the common commands you will used with lists in redis.
We can push items to a list using lpush
. The first param is
the name of the key and then we can pass in as many items as we
want for the list.
## Left Push
res = r.lpush("mylist", "two", "one")
print(res)
For each of the example below, I will use the following template to run all the commands. Here is my full index.js file. We will just replace the commands each time.
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
# Left Push
res = r.lpush("mylist", "two", "one")
print(res)
Similar to left push, we can add items to the list
using rpush
, but we can do this from the end or the
right side.
# Right Push
res = r.rpush("mylist", "three", "four")
print(res)
We can select a portion of our list using the lrange
command.
Here is an example select the first few items.
# List Range
res = r.lrange("mylist", 0, 2)
print(res)
We can also select the full list using -1 as the end index.
res = r.lrange("mylist", 0, -1)
print(res)
We can use the linsert
command to insert in a more specific manner. Here we use the "after" string in the second paramter to tell redis which key to insert after. So, in this case, we will insert the value "five" after the value "four".
# List Insert
res = r.linsert("mylist", "after", "four", "five")
print(res)
We can get an item at a specific index using the lindex
command.
# List Index
res = r.lindex("mylist", 2)
print(res)
To see the size of a list, use the llen
command.
# List Length
res = r.llen("mylist")
print(res)
The lpop
allows you to pop from the left or the start of a list.
# Left Pop
res = r.lpop("mylist")
print(res)
Similar to left pop, we can use rpop
to pop from the right side.
# Right Pop
res = r.rpop("mylist")
print(res)
We can trim a list using ltrim
. Here we tell redis to only keep indexes 1 to 3.
# List Trim
res = r.ltrim("mylist", 1, 3)
print(res)
If you prefer to set an item using a specific index, you can use lset
. In the example below, we set the value "foo" at index 1.
# List Set
res = r.lset("mylist", 1, "foo")
print(res)