Skip to content

Redis Data Type : List

Understand a list

Imagine a list looks like this: [B, C, D, E, F]

From left to right, ‘B’ is the first item and ‘F’ is the last item. From right to left, ‘B’ is the last one and ‘F’ is the first one. If I want to insert an item into the list or delete an. item from the list, I have to specify from which side.

In Redis, the terms “L” and “R” in commands like LPUSH, LPOP, LRANGE, etc., stand for “left” and “right” respectively. They indicate the direction from which items are pushed or popped from the list.

  • LPUSH: Adds one or more elements to the head (left side) of the list.
  • LPOP: Removes and returns the first element from the head (left side) of the list.
  • RPUSH: Adds one or more elements to the tail (right side) of the list.
  • RPOP: Removes and returns the last element from the tail (right side) of the list.
  • LRANGE: Retrieves elements from the list within the specified range, starting from the left side.

Add items to a list

You can add an item to a list or add multiple items at once by using LPUSH or RPUSH

For example, the list is called “fruits” and we will add some items.

127.0.0.1:6379> LPUSH fruits "Banana"
(integer) 1
127.0.0.1:6379> LPUSH fruits "Apple" "Ananas" "Watermelon" "Kiwi"
(integer) 5

Retrieve items from a list

  • You can get all items by using LRANGE. The 0 -1 means to get all items from the list.
  • Notice, the first item that has been added stays at the bottom and the last added item stays at the top.
  • If you want to add an item under “Banana”, you can use RPUSH
127.0.0.1:6379> LRANGE fruits 0 -1
1) "Kiwi"
2) "Watermelon"
3) "Ananas"
4) "Apple"
5) "Banana"
127.0.0.1:6379> RPUSH fruits "Peach"
(integer) 6
127.0.0.1:6379> LRANGE fruits 0 -1
1) "Kiwi"
2) "Watermelon"
3) "Ananas"
4) "Apple"
5) "Banana"
6) "Peach"

Besides getting the items of a list, you can get the length of a list,too.

127.0.0.1:6379> LLEN fruits
(integer) 6

Insert items into a list

Adding items into a list is a type of inserting. In this section I will show you how to insert an item before or after another specific item.

For example, I want to add “orange” before “Banana”.

127.0.0.1:6379> LINSERT fruits BEFORE Banana Orange
(integer) 7
127.0.0.1:6379>  LRANGE fruits 0 -1
1) "Kiwi"
2) "Watermelon"
3) "Ananas"
4) "Apple"
5) "Orange"
6) "Banana"
7) "Peach"

Delete items from a list

  • You can delete the first item or last item from a list by using LPOP or RPOP
  • You can delete any specific item from a list, as long as you know its index.

For example, I want to delete the first item “Kiwi” and last item “Peach”.

127.0.0.1:6379> LPOP fruits     
"Kiwi"
127.0.0.1:6379> RPOP fruits
"Peach"
127.0.0.1:6379> LRANGE fruits 0 -1
1) "Watermelon"
2) "Ananas"
3) "Apple"
4) "Orange"
5) "Banana"

To find out the index, you can use LINDEX. Notice, In a list, the index of first item is 0. In this example,”Ananas” is the second item and its index is 1.

127.0.0.1:6379> LINDEX fruits 1
"Ananas"

Remove the specific item “Ananas” by using LREM

127.0.0.1:6379> LREM fruits 0 "Ananas"
(integer) 1
127.0.0.1:6379> LRANGE fruits 0 -1
1) "Watermelon"
2) "Apple"
3) "Orange"
4) "Banana"
Tags: