LREM

Syntax

  1. LREM key count element

Time complexity: O(N+M) where N is the length of the list and M is the number of elements removed.

Removes the first count occurrences of elements equal to element from the list stored at key. The count argument influences the operation in the following ways:

  • count > 0: Remove elements equal to element moving from head to tail.
  • count < 0: Remove elements equal to element moving from tail to head.
  • count = 0: Remove all elements equal to element.

For example, LREM list -2 "hello" will remove the last two occurrences of "hello" in the list stored at list.

Note that non-existing keys are treated like empty lists, so when key does not exist, the command will always return 0.

Return

Integer reply: the number of removed elements.

Examples

  1. dragonfly> RPUSH mylist "hello"
  2. (integer) 1
  3. dragonfly> RPUSH mylist "hello"
  4. (integer) 2
  5. dragonfly> RPUSH mylist "foo"
  6. (integer) 3
  7. dragonfly> RPUSH mylist "hello"
  8. (integer) 4
  9. dragonfly> LREM mylist -2 "hello"
  10. (integer) 2
  11. dragonfly> LRANGE mylist 0 -1
  12. 1) "hello"
  13. 2) "foo"