Using this guide, you will learn how to work with lists in Redis, including creating, inserting, and deleting values.
In this guide, we have tested all the commands on the latest version of Redis. We recommend you do the same and use the native Redis CLI. Doing so will ensure maximum compatibility and allow you to see similar outputs to the ones in this guide.
Basics – Creating a List in Redis
Creating a list in Redis is a little more than creating a simple key and value pair. It is good to note that a key can hold a single list only.
As mentioned, Redis uses the concept of head and tail or left and right to manage a list.
There are two ways you can add items to a list:
The LPUSH commands add the specified new element to the list’s head (or left). The RPUSH command, on the other hand, will add the new list element to the tail (or right) of the specified list.
You use two main commands to create a new list or add items to an existing list.
Let us take a few examples.
To create a simple list called databases, we can use the command:
NOTE: You can also use RPUSH to perform the same operation.
Both LPUSH and RPUSH commands will return an integer value indicating the number of elements in the list.
Take the following examples to add more elements to the list.
127.0.0.1:6379> LPUSH databases Redis
127.0.0.1:6379> RPUSH databases PostgreSQL
127.0.0.1:6379> RPUSH databases MySQL
127.0.0.1:6379> LPUSH databases CockroachDB
You can add multiple items to a list in a single command. For example, we can substitute the above commands for one as:
The same case applies to the RPUSH command.
Redis also provides the LPUSHX and RPUSHX commands. They are used similar to the LPUSH and RPUSH commands; however, they cannot create a list. The key must exist before inserting elements with LPUSHX and RPUSHX commands.
127.0.0.1:6379> LPUSHX databases MariaDB
Update a List item
To modify a value of an item in a Redis list, use the LSET command. The command takes the list, index of the old element to update, and the new value.
For example, to change the value of the item at index 0, we can do:
The command returns the string “OK” if the command executes successfully.
Fetch Elements from a List
To fetch items from a list, use the LRANGE command. The command takes a start and stop index and returns the values within the specified range.
You can use a negative range. For example, -1 represents the last element in the list, and -4 represents the fourth to last element.
If you want to get a single element in the list, use the LINDEX command followed by the target index of the item to get.
Keep in mind that indexing starts at 0.
If you want to know the number of items in a list, use the LLEN command.
The command returns an integer representing the number of items in the list.
Deleting Items from a List
If you want to remove an item from a list, use the LREM command. The command takes the count and a value to remove.
The command will remove the first occurrence matching a specific pattern by default.
You can also use the LPOP and RPOP commands to remove items from a list. The commands remove the leftmost and rightmost elements in the list, respectively.
127.0.0.1:6379> RPOP databases
Both commands remove and return the value of the removed item.
Using this guide, you learned how to work with List in Redis. You can check the documentation to learn more about List commands and how they work.
Thank you for reading 🙂