Advertisement

In the previous blog, we learned about Redis Hashes, Commands to Set, Get, and Delete Hashes. This Time we will learn about Redis List Commands.

Lists are present in almost every programming language and play a very important role when it comes to storing information.

Redis an in-memory database also has a lists of data structures. Its structure is somewhat like linked lists and insertions and deletion can be done by both sides.

In Redis, lists are like normal lists and are arranged in their insertion orders.

Create Redis List

As stated earlier, Redis list insertion can be done from both sides i.e Head and Tail. When the list is seen horizontally the Head comes to the left and the Tail to the right. So the Redis Command to push elements towards Head is LPUSH and towards tail is RPUSH.

LPUSH/RPUSH key value [value ...]

$ 127.0.0.1:6379> lpush number 1 2 3 4 5
  (integer) 5

$ 127.0.0.1:6379> rpush number 1 2 3 4 5
  (integer) 5

Range List Values

Just like in programming languages where we can range over the List values, In the Redis list, we can range over the list items too.

lrange key start end

Ranging the LPUSHed Lists.

$ 127.0.0.1:6379> lpush number 1 2 3 4 5
  (integer) 5
$ 127.0.0.1:6379> lrange number 0 5
  1) "5"
  2) "4"
  3) "3"
  4) "2"
  5) "1"

In the Redis LPUSH, the elements are pushed to the head, and thus when looped the output is in reverse order.

Now, Ranging the RPUSHed List.

$ 127.0.0.1:6379> rpush number 1 2 3 4 5
  (integer) 5
$ 127.0.0.1:6379> lrange number 0 5
  1) "1"
  2) "2"
  3) "3"
  4) "4"
  5) "5"

You can notice the difference between the two outputs.

In the Redis RPUSH, the elements are pushed to the tail, and thus when looped the output is in the same order as they are pushed.

Range List without knowing the end

You have noticed that lrange requires the start and end value to range over a list. We always know the start i.e 0, but we always don’t know the end value.

lrange key 0 -1

$ 127.0.0.1:6379> lrange number 0 -1
  1) "1"
  2) "2"
  3) "3"
  4) "4"
  5) "5"

Redis List POP Command

If we can insert an item in a list then there should be a way to delete it. Redis lpop and rpop commands allow us to pop elements from a list.

LPOP/RPOP key

$ 127.0.0.1:6379> lpush number 1 2 3 4 5
  (integer) 5
$ 127.0.0.1:6379> lpop number
  "5"
$ 127.0.0.1:6379> rpop number
  "1"
$ 127.0.0.1:6379> lrange number 0 5
  1) "4"
  2) "3"
  3) "2"

Redis List Index

Redis List Indexing is the same as in programming languages, the index starts from 0.

lindex key index

$ 127.0.0.1:6379> lrange number 0 -1
  1) "4"
  2) "3"
  3) "2"
$ 127.0.0.1:6379> lindex number 0
  "4"

Insert into List at Index

we can also insert an element at any index using the Redis LSET Command.

LSET key index value

$ 127.0.0.1:6379> lset number 1 10
  OK
$ 127.0.0.1:6379> lrange number 0 -1
  1) "4"
  2) "10"
  3) "2"

The number of items remains the same but the element at index 1 is replaced with the new value.

Redis Insert After / Before in a list

Redis also provides us a command with which we can insert an element before or after any element in the list.

linsert key before/after pivot value

$ 127.0.0.1:6379> lrange number 0 -1
  1) "11"
  2) "4"
  3) "10"
  4) "2"
  5) "12"
$ 127.0.0.1:6379> linsert number before 2 55
  (integer) 6
$ 127.0.0.1:6379> linsert number after 2 77
  (integer) 7
$ 127.0.0.1:6379> lrange number 0 -1
  1) "11"
  2) "4"
  3) "10"
  4) "55"
  5) "2"
  6) "77"
  7) "12"

The pivot here is the actual number in the list where you want to insert the element.

Length of Redis List

Sometimes it is necessary to find the length of the list. In Redis, the length of the list is returned by Redis llen command.

llen key

$ 127.0.0.1:6379> llen number
  (integer) 3

LPUSHX/RPUSHX

Redis LPUSHX and RPUSHX work the same as LPUSH and RPUSH Command. The only thing it does extra is it first checks if the key/List exists or not. If the list exists it pushes the element else throws an error.

$ 127.0.0.1:6379> lpushx number 11
  (integer) 54
$ 127.0.0.1:6379> lrange number 0 -1
  1) "11"
  2) "4"
  3) "10"
  4) "2"

$ 127.0.0.1:6379> rpushx number 12
  (integer) 5
$ 127.0.0.1:6379> lrange number 0 -1
  1) "11"
  2) "4"
  3) "10"
  4) "2"
  5) "12"

If the Key doesn’t exist.

$ 127.0.0.1:6379> rpushx student 12
  (integer) 0

Integer 0 is returned if the key doesn’t exist.

Hope you like it!

Learn more about Redis List Commands from Redis official Documentation.

About Author
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top