Redis list is an array of string used to store array data structure in Redis under a key. I this lesson I will show the commands of the Redis list.
LPUSH command is used to create a new list or add an item at beginning of the list:
Syntax:
LPUSH key value ...
Example:
LPUSH numbers 1 345 45 234 234
Some Other Commands of List:
Commands Description | Syntax | Example |
---|---|---|
Remove and get first element of a list | LPOP key | LPOP numbers |
Remove and get last element of a list | RPOP key | RPOP numbers |
Get element by index | LINDEX key index | LINDEX numbers 1 |
Adding item to left (beginning) of the list | LPUSH key value | LPUSH numbers 24 |
Adding item to right (last) of the list | RPUSH key value | RPUSH numbers 30 |
Insert an item before or after another item in list | LINSERT key BEFORE|AFTER pivot value | LINSERT numbers AFTER 28 100 |
Get the length of a list | LLEN key | LLEN numbers |
Add item to left only if the list is exists | LPUSHX key value | LPUSHX numbers 2 |
Getting value of a rage | LRANGE key start stop | LRANGE numbers 0 6 // Get from 0 to 6 LRANGE numbers 0 -1 // Get all data |
Remove Element form list | LREM key count value | LREM numbers 0 23 // Remove all LREM numbers -1 23 // Remove one from right side LREM numbers 1 23 // Remove one from left side |
Set the value by idex | LSET key index value | LSET numbers 1 20 |
RPOP form source and LPUSH to destination | RPOPLPUSH source destination | RPOPLPUSH numbers new |
Append data to list right side only if the list exists | RPUSHX key value | RPUSHX numbers 333 |