Working with Lists
1. Pushing Elements
| Command | Direction | Returns |
|---|---|---|
LPUSH key v [v ...] | Head | New length |
RPUSH key v [v ...] | Tail | New length |
2. Pushing if Exists
| Command | Description |
|---|---|
LPUSHX key v | Push to head only if list exists |
RPUSHX key v | Push to tail only if list exists |
3. Popping Elements
| Command | Description |
|---|---|
LPOP key [count] | Pop from head; count returns array |
RPOP key [count] | Pop from tail |
4. Getting List Length
| Command | Returns |
|---|---|
LLEN key | Element count; 0 if missing |
5. Getting Elements by Range
LRANGE queue 0 -1 # all
LRANGE queue 0 9 # first 10
LRANGE queue -5 -1 # last 5
| Command | Notes |
|---|---|
LRANGE key start stop | Inclusive; supports negative indices |
6. Getting Elements by Index
| Command | Notes |
|---|---|
LINDEX key index | Single element; O(N) worst-case |
7. Setting Elements by Index
| Command | Errors |
|---|---|
LSET key index value | Error if index out of range |
8. Inserting Elements
| Command | Returns |
|---|---|
LINSERT key BEFORE|AFTER pivot value | New length, -1 if pivot missing |
9. Trimming Lists
| Command | Effect |
|---|---|
LTRIM key start stop | Keep only the range; removes everything else |
10. Removing Elements
| Command | Count Semantics |
|---|---|
LREM key count value | >0 head→tail, <0 tail→head, 0 all |
11. Getting List Position
| Command | Options |
|---|---|
LPOS key element [RANK r] [COUNT c] [MAXLEN m] | Index of match; COUNT 0 for all 6.0.6+ |
12. Using LMOVE for Element Transfer
| Command | Use |
|---|---|
LMOVE src dst LEFT|RIGHT LEFT|RIGHT | Atomic pop+push; reliable queue pattern |
BLMOVE src dst LEFT|RIGHT LEFT|RIGHT timeout | Blocking variant |
13. Using LMPOP for Multiple Pops
| Command | Description |
|---|---|
LMPOP numkeys k1 ... LEFT|RIGHT [COUNT n] | Pop from first non-empty list 7.0+ |
BLMPOP timeout numkeys ... LEFT|RIGHT [COUNT n] | Blocking variant |