- List is a Sequence of array or ordered elements.
- You can insert a element from head and tail.
- The order of elements in list depend upon on the elements insertion sequence.
1. How to create a key with list data type.
Create List key value: LPUSH/RPUSH <key> "<value1>" "<value2>" "<value3>"...
Get String key value : LRANGE <key> <start indexnumber> <end indexnumber>
Check the type of data type: TYPE<key> <value>
Fetch the element of particular index: LINDEX <key> <indexnumber>
pawan@LAPTOP-SG31RIME:/mnt/d/PAWAN$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379> lpush dbtype "Postgres" "Redis" "ES"
(integer) 3
127.0.0.1:6379> lrange dbtype 0 1 // fetch specific index elements from list
1) "ES"
2) "Redis"
127.0.0.1:6379> lrange dbtype 0 -1 // fetch all elements from list
1) "ES"
2) "Redis"
3) "Postgres"
127.0.0.1:6379>
127.0.0.1:6379> type dbtype
list
127.0.0.1:6379> lpush dbtype "Oracle" // lpush will insert a record on top of the list
(integer) 4
127.0.0.1:6379>
127.0.0.1:6379> lrange dbtype 0 -1
1) "Oracle"
2) "ES"
3) "Redis"
4) "Postgres"
127.0.0.1:6379> rpush dbtype "MSSQL" // rpush will insert a record on bottom or end of the list
(integer) 5
127.0.0.1:6379> lrange dbtype 0 -1
1) "Oracle"
2) "ES"
3) "Redis"
4) "Postgres"
5) "MSSQL"
127.0.0.1:6379>
127.0.0.1:6379> lindex dbtype 2 // fetch 3rd element
"Redis"
127.0.0.1:6379> lindex dbtype 0 // fetch 1st element
"Oracle"
127.0.0.1:6379> lindex dbtype -1 // fetch last element
"MSSQL"
127.0.0.1:6379>
2. Add element to specific(before or after) position.
LINSERT <key> <BEFORE| AFTER> "<value>" "<new value>"
pawan@LAPTOP-SG31RIME:/mnt/d/PAWAN$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379> lrange dbtype 0 -1
1) "Oracle"
2) "ES"
3) "Redis"
4) "Postgres"
5) "MSSQL"
127.0.0.1:6379> linsert dbtype before "Postgres" "SQLSERVER"
(integer) 6
127.0.0.1:6379>
127.0.0.1:6379> lrange dbtype 0 -1
1) "Oracle"
2) "ES"
3) "Redis"
4) "SQLSERVER"
5) "Postgres"
6) "MSSQL"
127.0.0.1:6379> linsert dbtype after "Postgres" "MONGO"
(integer) 7
127.0.0.1:6379> lrange dbtype 0 -1
1) "Oracle"
2) "ES"
3) "Redis"
4) "SQLSERVER"
5) "Postgres"
6) "MONGO"
7) "MSSQL"
127.0.0.1:6379>
3. Remove element from list
Remove the element from top or left hand side and Remove only one element : LPOP <key>
Remove the element from top or left hand side and Remove only one element : RPOP <key>
Remove the element from top or left hand side and Remove only defined count element : LPOP <key> <count>
Remove the element from top or left hand side and Remove only defined count element : RPOP <key> <count>pawan@LAPTOP-SG31RIME:/mnt/d/PAWAN$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> lrange dbtype 0 -1 // List the element in dbtype
1) "Oracle"
2) "ES"
3) "Redis"
4) "SQLSERVER"
5) "Postgres"
6) "MONGO"
7) "MSSQL"
127.0.0.1:6379>
127.0.0.1:6379> lpop dbtype // remove 1 element from top of the list
"Oracle"
127.0.0.1:6379> lrange dbtype 0 -1
1) "ES"
2) "Redis"
3) "SQLSERVER"
4) "Postgres"
5) "MONGO"
6) "MSSQL"
127.0.0.1:6379> rpop dbtype // remove 1 element from bottom of the list
"MSSQL"
127.0.0.1:6379> lrange dbtype 0 -1
1) "ES"
2) "Redis"
3) "SQLSERVER"
4) "Postgres"
5) "MONGO"
127.0.0.1:6379> lpop dbtype 2 // remove 2 element from top of the list
1) "ES"
2) "Redis"
127.0.0.1:6379> lrange dbtype 0 -1
1) "SQLSERVER"
2) "Postgres"
3) "MONGO"
127.0.0.1:6379> rpop dbtype 1 // remove 1 element from top of the list
1) "MONGO"
127.0.0.1:6379> lrange dbtype 0 -1
1) "SQLSERVER"
2) "Postgres"
127.0.0.1:6379>
4. Remove element from list or trim the list
define the range of element you want to keep, it will remove all element comes out of defined rangge.
LTRIM <key> <start> <end>
127.0.0.1:6379> lrange dbtype 0 -1
1) "TimescaleDB"
2) "Oracle"
3) "SQLSERVER"
4) "MONGO"
5) "ES"
6) "Redis"
7) "Postgres"
127.0.0.1:6379>
127.0.0.1:6379> ltrim dbtype 2 -1 // removed 0, 1 elements from top of the list as range is 2 to -1
OK
127.0.0.1:6379> lrange dbtype 0 -1
1) "SQLSERVER"
2) "MONGO"
3) "ES"
4) "Redis"
5) "Postgres"
127.0.0.1:6379> ltrim dbtype 2 -2 // removed 0, 1, -1 elements from top of the list as range is 2 to -2
OK
127.0.0.1:6379> lrange dbtype 0 -1
1) "ES"
2) "Redis"
127.0.0.1:6379>
5. Set the value of specific index and find out length of list
LSET <key> <index> <>value>
LLEN <key>
127.0.0.1:6379> lpush dbtype "Postgres" "MONGO" "SQLSERVER" "Oracle" "TimescaleDB"
(integer) 5
127.0.0.1:6379> lrange dbtype 0 -1
1) "TimescaleDB"
2) "Oracle"
3) "SQLSERVER"
4) "MONGO"
5) "Postgres"
127.0.0.1:6379> lset dbtype 2 MSSQLSERVER // set the new value of SQLSERVER to MSSQLSERVER
OK
127.0.0.1:6379> lrange dbtype 0 -1
1) "TimescaleDB"
2) "Oracle"
3) "MSSQLSERVER"
4) "MONGO"
5) "Postgres"
127.0.0.1:6379>
127.0.0.1:6379> llen dbtype
(integer) 5
6. Find the element from list using LPOS.
LPOS <key> <element> rank <index>
LPOS <key> <element> rank <index> count <count>
pawan@LAPTOP-SG31RIME:/mnt/d/PAWAN$ redis-cli
127.0.0.1:6379> lpush key a b c a d a e f g h a
(integer) 11
127.0.0.1:6379> lrange key 0 -1 // List the element in dbtype
1) "a"
2) "h"
3) "g"
4) "f"
5) "e"
6) "a"
7) "d"
8) "a"
9) "c"
10) "b"
11) "a"
127.0.0.1:6379>
127.0.0.1:6379> lpos key "a" rank 1 // List the alphabet "a" from top
(integer) 0
127.0.0.1:6379> lpos key "a" rank 2 // List the alphabet "a" from second from top
(integer) 5
127.0.0.1:6379> lpos key "a" rank -1 // List the alphabet "a" from bottom
(integer) 10
127.0.0.1:6379> lpos key "a" rank -2 // List the alphabet "a" from second from bottom
(integer) 7
127.0.0.1:6379> lpos key "a" rank 1 count 0 // List the all alphabet "a" from top to bottom
1) (integer) 0
2) (integer) 5
3) (integer) 7
4) (integer) 10
127.0.0.1:6379> lpos key "a" rank -1 count 0 // List the all alphabet "a" from bottom to top
1) (integer) 10
2) (integer) 7
3) (integer) 5
4) (integer) 0
127.0.0.1:6379> lpos key "a" rank -1 count 2 // List the only two alphabet "a" from bottom to top
1) (integer) 10
2) (integer) 7
127.0.0.1:6379>
6. Remove the element based of number of occurrences.
LREM <key> <count> <element>
count: 0 -- all
count: 1 (top to bottom)
count: -1 (bottom to top)
127.0.0.1:6379> lpush key a b c a d a e f g h a
(integer) 11
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> lrange key 0 -1
1) "a"
2) "h"
3) "g"
4) "f"
5) "e"
6) "a"
7) "d"
8) "a"
9) "c"
10) "b"
11) "a"
127.0.0.1:6379>
127.0.0.1:6379> lrem key 1 "a"
(integer) 1
127.0.0.1:6379> lrange key 0 -1
1) "h"
2) "g"
3) "f"
4) "e"
5) "a"
6) "d"
7) "a"
8) "c"
9) "b"
10) "a"
127.0.0.1:6379> lrem key -1 "a"
(integer) 1
127.0.0.1:6379> lrange key 0 -1
1) "h"
2) "g"
3) "f"
4) "e"
5) "a"
6) "d"
7) "a"
8) "c"
9) "b"
127.0.0.1:6379> lrem key 0 "a"
(integer) 2
127.0.0.1:6379> lrange key 0 -1
1) "h"
2) "g"
3) "f"
4) "e"
5) "d"
6) "c"
7) "b"
127.0.0.1:6379>