Working with Lists

1. Pushing Elements

CommandDirectionReturns
LPUSH key v [v ...]HeadNew length
RPUSH key v [v ...]TailNew length

2. Pushing if Exists

CommandDescription
LPUSHX key vPush to head only if list exists
RPUSHX key vPush to tail only if list exists

3. Popping Elements

CommandDescription
LPOP key [count]Pop from head; count returns array
RPOP key [count]Pop from tail

4. Getting List Length

CommandReturns
LLEN keyElement 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
CommandNotes
LRANGE key start stopInclusive; supports negative indices

6. Getting Elements by Index

CommandNotes
LINDEX key indexSingle element; O(N) worst-case

7. Setting Elements by Index

CommandErrors
LSET key index valueError if index out of range

8. Inserting Elements

CommandReturns
LINSERT key BEFORE|AFTER pivot valueNew length, -1 if pivot missing

9. Trimming Lists

CommandEffect
LTRIM key start stopKeep only the range; removes everything else

10. Removing Elements

CommandCount Semantics
LREM key count value>0 head→tail, <0 tail→head, 0 all

11. Getting List Position

CommandOptions
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

CommandUse
LMOVE src dst LEFT|RIGHT LEFT|RIGHTAtomic pop+push; reliable queue pattern
BLMOVE src dst LEFT|RIGHT LEFT|RIGHT timeoutBlocking variant

Example: Reliable worker queue

LMOVE jobs:pending jobs:processing RIGHT LEFT

13. Using LMPOP for Multiple Pops

CommandDescription
LMPOP numkeys k1 ... LEFT|RIGHT [COUNT n]Pop from first non-empty list 7.0+
BLMPOP timeout numkeys ... LEFT|RIGHT [COUNT n]Blocking variant