Python Redis Lists



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.

Setting up Redis

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"
    image: "redis:alpine"
    command: redis-server
      - "6379:6379"
      - $PWD/redis-data:/var/lib/redis
      - $PWD/redis.conf:/usr/local/etc/redis/redis.conf

Ensure you have docker installed and run

docker-compose up

Installing Redis Modules

In python, the main used redis module is called redis-py and can be installed using the follows.

pip install redis

Writing the Code

Let's open up a new file, and go through many of the common commands you will used with lists in redis.

Left Push

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")

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")

Right Push

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")

List Range

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)

We can also select the full list using -1 as the end index.

res = r.lrange("mylist", 0, -1)

List Insert

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")

List Index

We can get an item at a specific index using the lindex command.

# List Index
res = r.lindex("mylist", 2)

List Length

To see the size of a list, use the llen command.

# List Length 
res = r.llen("mylist")

Left Pop

The lpop allows you to pop from the left or the start of a list.

# Left Pop 
res = r.lpop("mylist")

Right Pop

Similar to left pop, we can use rpop to pop from the right side.

# Right Pop 
res = r.rpop("mylist")

List Trim

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)

List Set

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")