...

/

List Operations with Redis Commands

List Operations with Redis Commands

Learn and practice frequently used Redis commands to store list data in the Redis data store.

We’re going to explore the different commands that can be executed when dealing with lists in Redis. We’ll learn the five most commonly used Redis commands to store and retrieve data in a list format, which can be of great help in creating applications where we want to work with a list of items.

The LPUSH command

The LPUSH command is used to insert all the values provided in the command to the start (head) of the list. This command creates a new list with the given values, even if the key isn’t present. All the values are inserted from the left to the right in the list. The LPUSH command returns an integer denoting the length of the list after insertion.

Syntax and example

The syntax of the LPUSH command is shown below:

LPUSH key value1 value2 value3 ...
Syntax of using LPUSH command

An example of how to store multiple values in a list named courses is given below:

LPUSH courses "java" "python" "javascript" "react"
Example of using LPUSH command

The command above creates an empty list, stores the values, and returns the integer 4 (the length of the list after insertion) as the output.

The RPUSH command

This command is very similar to the previous LPUSH command discussed above. The only difference is that the insertion of the values happens from the end (tail) of the list.

Syntax and example

The syntax of the RPUSH command is shown below:

RPUSH key value1 value2 value3 ...
Syntax of using RPUSH command

We’ll use the same example discussed above. Although we won’t see any difference in the output, later on, when we access the elements from the list, there will be a difference in the order of the elements. That's because the LPUSH command inserts from the start, whereas the RPUSH command inserts from the end of the list.

RPUSH courses "java" "python" "javascript" "react"
Example of using RPUSH command

The command above creates an empty list, ...