Skip to content

Redis - Lists

Redis lists are lists of strings, sorted by the insertion order. List can contain duplicate values as it does not enforce unique values.

Learn More

Limitations with lists

  • You cannot find if a given value exists in a list. If you would need to do so, it would be best to use some of the other data types in Redis.
  • You cannot nest other data structure inside a list other than a string. So for example, creating a list and nesting another list or hash inside that list is not possible.

Adding Elements to a List (LPUSH, RPUSH)

To add elements into a list (and to create a list with the given key), the commands LPUSH and RPUSH can be utilized. The RPUSH command adds the value to the end of the list and the LPUSH command adds the value to the beginning of the list.

Time Complexity: O(1) for each element added, so O(N) when there are multiple values.

For Example:

Text Only
RPUSH user#2:purchases:amount 30,99 15
Example Output
integer (2)
Text Only
LPUSH user#2:purchases:amount 0.99 9150
LPUSH user#2:purchases:amount 150
Example Output
integer (4)
integer (5)

Listing Elements in a list (LRANGE)

To list the elements in a list (in the given range), the command LRANGE can be used. Learn more

Time Complexity: O(S+N) where S is the offset and N is the number of elements.

For example:

By making the ending index to be -1, all elements will be listed:

Text Only
LRANGE user#2:purchases:amount 0 -1
Example Output
1) "150"
2) "9150"
3) "0.99"
4) "30,99"
5) "15"

To list items in the range of 0 to 2:

Text Only
LRANGE user#2:purchases:amount 0 2
Example Output
1) "150"
2) "9150"
3) "0.99"

List Length (LLEN)

To list the length of the list, the command LLEN can be used. Learn more

Time Complexity: O(1)

For example:

Text Only
LLEN user#2:purchases:amount
Example Output
integer (5)

Adding Item Before / After Specified Item (LINSERT)

To add a new item before or after the specified value in the list, the command LINSERT can be used. Learn more

Time Complexity: O(1) when inserting to beginning or ending of the list. If inserting to any other position, O(N) where N is the amount of elements that need to be traversed to find the pivot element.

For example:

Text Only
LINSERT user#2:purchases:amount AFTER 30,99 89
LINSERT user#2:purchases:amount BEFORE 89 505
Example Output
(integer) 6
(integer) 7

The first command would find the value 30,99 in the list and add the given value after that item. The second command does the same but adds the new value before the item.

Getting Item by Index (LINDEX)

To get the item in a list with the given index, the command LINDEX can be used. Learn more

Time Complexity: O(1) for accessing first or last element of the list. If accessing any other element from the list, O(N) where N is the amount of elements that need to be traversed to find the element.

For example:

Text Only
LINDEX user#2:purchases:amount 1
Example Output
"9150"

Removing the First and Last Items (LPOP, RPOP)

To remove (pop) element from the beginning or end of the list, the commands LPOP and RPOP can be used. LPOP removes item from the beginning of the list (left pop) and RPOP removes item from the ending of the list (right pop).

Time Complexity: O(N) where N is the amount of items returned. O(1) if it is only one item.

For example:

Text Only
LPOP user#2:purchases:amount
Example Output
"150"
Text Only
RPOP user#2:purchases:amount
Example Output
"15"

Trimming List to Specified Range (LTRIM)

To trim the list to specified range, the command LTRIM can be used. For this command you pass the starting and ending index. All other values will be removed from the list. Learn more

Time Complexity: O(N) where N is the amount of elements to be removed. O(1) if only one element will be removed.

For example:

Text Only
LTRIM user#2:purchases:amount 0 2
Example Output
OK